Skip to content

Remove object literal freshness in control flow based array types #39518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20633,7 +20633,7 @@ namespace ts {
// we defer subtype reduction until the evolving array type is finalized into a manifest
// array type.
function addEvolvingArrayElementType(evolvingArrayType: EvolvingArrayType, node: Expression): EvolvingArrayType {
const elementType = getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node));
const elementType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node)));
return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
}

Expand Down
14 changes: 13 additions & 1 deletion tests/baselines/reference/controlFlowArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,16 @@ function f18() {
x.unshift("hello");
x[2] = true;
return x; // (string | number | boolean)[]
}
}

// Repro from #39470

declare function foo(arg: { val: number }[]): void;

let arr = []
arr.push({ val: 1, bar: 2 });
foo(arr);


//// [controlFlowArrays.js]
function f1() {
Expand Down Expand Up @@ -340,3 +349,6 @@ function f18() {
x[2] = true;
return x; // (string | number | boolean)[]
}
var arr = [];
arr.push({ val: 1, bar: 2 });
foo(arr);
22 changes: 22 additions & 0 deletions tests/baselines/reference/controlFlowArrays.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,25 @@ function f18() {
return x; // (string | number | boolean)[]
>x : Symbol(x, Decl(controlFlowArrays.ts, 172, 7))
}

// Repro from #39470

declare function foo(arg: { val: number }[]): void;
>foo : Symbol(foo, Decl(controlFlowArrays.ts, 177, 1))
>arg : Symbol(arg, Decl(controlFlowArrays.ts, 181, 21))
>val : Symbol(val, Decl(controlFlowArrays.ts, 181, 27))

let arr = []
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))

arr.push({ val: 1, bar: 2 });
>arr.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>val : Symbol(val, Decl(controlFlowArrays.ts, 184, 10))
>bar : Symbol(bar, Decl(controlFlowArrays.ts, 184, 18))

foo(arr);
>foo : Symbol(foo, Decl(controlFlowArrays.ts, 177, 1))
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))

28 changes: 28 additions & 0 deletions tests/baselines/reference/controlFlowArrays.types
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,31 @@ function f18() {
return x; // (string | number | boolean)[]
>x : (string | number | boolean)[]
}

// Repro from #39470

declare function foo(arg: { val: number }[]): void;
>foo : (arg: { val: number;}[]) => void
>arg : { val: number; }[]
>val : number

let arr = []
>arr : any[]
>[] : never[]

arr.push({ val: 1, bar: 2 });
>arr.push({ val: 1, bar: 2 }) : number
>arr.push : (...items: any[]) => number
>arr : any[]
>push : (...items: any[]) => number
>{ val: 1, bar: 2 } : { val: number; bar: number; }
>val : number
>1 : 1
>bar : number
>2 : 2

foo(arr);
>foo(arr) : void
>foo : (arg: { val: number; }[]) => void
>arr : { val: number; bar: number; }[]

10 changes: 9 additions & 1 deletion tests/cases/compiler/controlFlowArrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,12 @@ function f18() {
x.unshift("hello");
x[2] = true;
return x; // (string | number | boolean)[]
}
}

// Repro from #39470

declare function foo(arg: { val: number }[]): void;

let arr = []
arr.push({ val: 1, bar: 2 });
foo(arr);