Skip to content

Commit c335aad

Browse files
authored
Remove object literal freshness in control flow based array types (microsoft#39518)
* Remove object literal freshness in control flow based array types * Add regression test
1 parent 87a74aa commit c335aad

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20633,7 +20633,7 @@ namespace ts {
2063320633
// we defer subtype reduction until the evolving array type is finalized into a manifest
2063420634
// array type.
2063520635
function addEvolvingArrayElementType(evolvingArrayType: EvolvingArrayType, node: Expression): EvolvingArrayType {
20636-
const elementType = getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node));
20636+
const elementType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(getContextFreeTypeOfExpression(node)));
2063720637
return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
2063820638
}
2063920639

tests/baselines/reference/controlFlowArrays.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,16 @@ function f18() {
176176
x.unshift("hello");
177177
x[2] = true;
178178
return x; // (string | number | boolean)[]
179-
}
179+
}
180+
181+
// Repro from #39470
182+
183+
declare function foo(arg: { val: number }[]): void;
184+
185+
let arr = []
186+
arr.push({ val: 1, bar: 2 });
187+
foo(arr);
188+
180189

181190
//// [controlFlowArrays.js]
182191
function f1() {
@@ -340,3 +349,6 @@ function f18() {
340349
x[2] = true;
341350
return x; // (string | number | boolean)[]
342351
}
352+
var arr = [];
353+
arr.push({ val: 1, bar: 2 });
354+
foo(arr);

tests/baselines/reference/controlFlowArrays.symbols

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,25 @@ function f18() {
467467
return x; // (string | number | boolean)[]
468468
>x : Symbol(x, Decl(controlFlowArrays.ts, 172, 7))
469469
}
470+
471+
// Repro from #39470
472+
473+
declare function foo(arg: { val: number }[]): void;
474+
>foo : Symbol(foo, Decl(controlFlowArrays.ts, 177, 1))
475+
>arg : Symbol(arg, Decl(controlFlowArrays.ts, 181, 21))
476+
>val : Symbol(val, Decl(controlFlowArrays.ts, 181, 27))
477+
478+
let arr = []
479+
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))
480+
481+
arr.push({ val: 1, bar: 2 });
482+
>arr.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
483+
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))
484+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
485+
>val : Symbol(val, Decl(controlFlowArrays.ts, 184, 10))
486+
>bar : Symbol(bar, Decl(controlFlowArrays.ts, 184, 18))
487+
488+
foo(arr);
489+
>foo : Symbol(foo, Decl(controlFlowArrays.ts, 177, 1))
490+
>arr : Symbol(arr, Decl(controlFlowArrays.ts, 183, 3))
491+

tests/baselines/reference/controlFlowArrays.types

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,31 @@ function f18() {
612612
return x; // (string | number | boolean)[]
613613
>x : (string | number | boolean)[]
614614
}
615+
616+
// Repro from #39470
617+
618+
declare function foo(arg: { val: number }[]): void;
619+
>foo : (arg: { val: number;}[]) => void
620+
>arg : { val: number; }[]
621+
>val : number
622+
623+
let arr = []
624+
>arr : any[]
625+
>[] : never[]
626+
627+
arr.push({ val: 1, bar: 2 });
628+
>arr.push({ val: 1, bar: 2 }) : number
629+
>arr.push : (...items: any[]) => number
630+
>arr : any[]
631+
>push : (...items: any[]) => number
632+
>{ val: 1, bar: 2 } : { val: number; bar: number; }
633+
>val : number
634+
>1 : 1
635+
>bar : number
636+
>2 : 2
637+
638+
foo(arr);
639+
>foo(arr) : void
640+
>foo : (arg: { val: number; }[]) => void
641+
>arr : { val: number; bar: number; }[]
642+

tests/cases/compiler/controlFlowArrays.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,12 @@ function f18() {
178178
x.unshift("hello");
179179
x[2] = true;
180180
return x; // (string | number | boolean)[]
181-
}
181+
}
182+
183+
// Repro from #39470
184+
185+
declare function foo(arg: { val: number }[]): void;
186+
187+
let arr = []
188+
arr.push({ val: 1, bar: 2 });
189+
foo(arr);

0 commit comments

Comments
 (0)