Skip to content

Commit 8e14031

Browse files
authored
Merge pull request #29192 from Microsoft/initializerWidening
Fix initializer widening
2 parents 4a664d6 + 73b1dcb commit 8e14031

6 files changed

+57
-8
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4919,7 +4919,7 @@ namespace ts {
49194919
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkDeclarationInitializer(declaration)) & TypeFlags.Undefined)) {
49204920
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
49214921
}
4922-
return declaration.initializer && !getContextualTypeForVariableLikeDeclaration(walkUpBindingElementsAndPatterns(declaration)) ?
4922+
return declaration.initializer && !getEffectiveTypeAnnotationNode(walkUpBindingElementsAndPatterns(declaration)) ?
49234923
getUnionType([type, checkDeclarationInitializer(declaration)], UnionReduction.Subtype) :
49244924
type;
49254925
}
@@ -22810,7 +22810,8 @@ namespace ts {
2281022810
const type = getTypeOfExpression(initializer, /*cache*/ true);
2281122811
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const ||
2281222812
isDeclarationReadonly(declaration) ||
22813-
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
22813+
isTypeAssertion(initializer) ||
22814+
isLiteralOfContextualType(type, getContextualType(initializer)) ? type : getWidenedLiteralType(type);
2281422815
if (isInJSFile(declaration)) {
2281522816
if (widened.flags & TypeFlags.Nullable) {
2281622817
reportImplicitAny(declaration, anyType);

tests/baselines/reference/destructuringArrayBindingPatternAndAssignment3.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const [f, g = f, h = i, i = f] = [1]; // error for h = i
4545
>c : number
4646
>d : number
4747
>c : number
48-
>e : number
49-
>e : number
48+
>e : any
49+
>e : any
5050

5151
})([1]);
5252
>[1] : number[]

tests/baselines/reference/destructuringInitializerContextualTypeFromContext.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ const Child: SFC<Props> = ({
1717
children,
1818
name = "Artemis",
1919
...props
20-
}) => `name: ${name} props: ${JSON.stringify(props)}`;
20+
}) => `name: ${name} props: ${JSON.stringify(props)}`;
21+
22+
// Repro from #29189
23+
24+
declare function f(g: (as: string[]) => void): void
25+
f(([_1, _2 = undefined]) => undefined)
26+
2127

2228
//// [destructuringInitializerContextualTypeFromContext.js]
29+
"use strict";
2330
var __assign = (this && this.__assign) || function () {
2431
__assign = Object.assign || function(t) {
2532
for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -48,3 +55,7 @@ var Child = function (_a) {
4855
var children = _a.children, _b = _a.name, name = _b === void 0 ? "Artemis" : _b, props = __rest(_a, ["children", "name"]);
4956
return "name: " + name + " props: " + JSON.stringify(props);
5057
};
58+
f(function (_a) {
59+
var _1 = _a[0], _b = _a[1], _2 = _b === void 0 ? undefined : _b;
60+
return undefined;
61+
});

tests/baselines/reference/destructuringInitializerContextualTypeFromContext.symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ const Child: SFC<Props> = ({
5656
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
5757
>props : Symbol(props, Decl(destructuringInitializerContextualTypeFromContext.ts, 16, 21))
5858

59+
// Repro from #29189
60+
61+
declare function f(g: (as: string[]) => void): void
62+
>f : Symbol(f, Decl(destructuringInitializerContextualTypeFromContext.ts, 18, 54))
63+
>g : Symbol(g, Decl(destructuringInitializerContextualTypeFromContext.ts, 22, 19))
64+
>as : Symbol(as, Decl(destructuringInitializerContextualTypeFromContext.ts, 22, 23))
65+
66+
f(([_1, _2 = undefined]) => undefined)
67+
>f : Symbol(f, Decl(destructuringInitializerContextualTypeFromContext.ts, 18, 54))
68+
>_1 : Symbol(_1, Decl(destructuringInitializerContextualTypeFromContext.ts, 23, 4))
69+
>_2 : Symbol(_2, Decl(destructuringInitializerContextualTypeFromContext.ts, 23, 7))
70+
>undefined : Symbol(undefined)
71+
>undefined : Symbol(undefined)
72+

tests/baselines/reference/destructuringInitializerContextualTypeFromContext.types

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,24 @@ const Child: SFC<Props> = ({
5050
>`name: ${name} props: ${JSON.stringify(props)}` : string
5151
>name : "Apollo" | "Artemis" | "Dionysus" | "Persephone"
5252
>JSON.stringify(props) : string
53-
>JSON.stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
53+
>JSON.stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
5454
>JSON : JSON
55-
>stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
55+
>stringify : { (value: any, replacer?: ((key: string, value: any) => any) | undefined, space?: string | number | undefined): string; (value: any, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string; }
5656
>props : {}
5757

58+
// Repro from #29189
59+
60+
declare function f(g: (as: string[]) => void): void
61+
>f : (g: (as: string[]) => void) => void
62+
>g : (as: string[]) => void
63+
>as : string[]
64+
65+
f(([_1, _2 = undefined]) => undefined)
66+
>f(([_1, _2 = undefined]) => undefined) : void
67+
>f : (g: (as: string[]) => void) => void
68+
>([_1, _2 = undefined]) => undefined : ([_1, _2]: string[]) => undefined
69+
>_1 : string
70+
>_2 : string | undefined
71+
>undefined : undefined
72+
>undefined : undefined
73+

tests/cases/compiler/destructuringInitializerContextualTypeFromContext.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @strict: true
2+
13
interface SFC<P = {}> {
24
(props: P & { children?: any }): any | null;
35
}
@@ -16,4 +18,9 @@ const Child: SFC<Props> = ({
1618
children,
1719
name = "Artemis",
1820
...props
19-
}) => `name: ${name} props: ${JSON.stringify(props)}`;
21+
}) => `name: ${name} props: ${JSON.stringify(props)}`;
22+
23+
// Repro from #29189
24+
25+
declare function f(g: (as: string[]) => void): void
26+
f(([_1, _2 = undefined]) => undefined)

0 commit comments

Comments
 (0)