Skip to content

Fix initializer widening #29192

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 4 commits into from
Dec 29, 2018
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
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4919,7 +4919,7 @@ namespace ts {
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkDeclarationInitializer(declaration)) & TypeFlags.Undefined)) {
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
}
return declaration.initializer && !getContextualTypeForVariableLikeDeclaration(walkUpBindingElementsAndPatterns(declaration)) ?
return declaration.initializer && !getEffectiveTypeAnnotationNode(walkUpBindingElementsAndPatterns(declaration)) ?
Copy link
Member

@weswigham weswigham Dec 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I think this is right to change. Outside of strict mode, I believe this is going to make a destructured variable with an undefined initializer (with a contextual type) be any instead of the correct contextual type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the right change (it basically just reverts the change that you had made). In classic checking mode this makes a variable of type string as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aight, ok then

getUnionType([type, checkDeclarationInitializer(declaration)], UnionReduction.Subtype) :
type;
}
Expand Down Expand Up @@ -22810,7 +22810,8 @@ namespace ts {
const type = getTypeOfExpression(initializer, /*cache*/ true);
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const ||
isDeclarationReadonly(declaration) ||
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
isTypeAssertion(initializer) ||
isLiteralOfContextualType(type, getContextualType(initializer)) ? type : getWidenedLiteralType(type);
if (isInJSFile(declaration)) {
if (widened.flags & TypeFlags.Nullable) {
reportImplicitAny(declaration, anyType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const [f, g = f, h = i, i = f] = [1]; // error for h = i
>c : number
>d : number
>c : number
>e : number
>e : number
>e : any
>e : any

})([1]);
>[1] : number[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ const Child: SFC<Props> = ({
children,
name = "Artemis",
...props
}) => `name: ${name} props: ${JSON.stringify(props)}`;
}) => `name: ${name} props: ${JSON.stringify(props)}`;

// Repro from #29189

declare function f(g: (as: string[]) => void): void
f(([_1, _2 = undefined]) => undefined)


//// [destructuringInitializerContextualTypeFromContext.js]
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
Expand Down Expand Up @@ -48,3 +55,7 @@ var Child = function (_a) {
var children = _a.children, _b = _a.name, name = _b === void 0 ? "Artemis" : _b, props = __rest(_a, ["children", "name"]);
return "name: " + name + " props: " + JSON.stringify(props);
};
f(function (_a) {
var _1 = _a[0], _b = _a[1], _2 = _b === void 0 ? undefined : _b;
return undefined;
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ const Child: SFC<Props> = ({
>stringify : Symbol(JSON.stringify, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>props : Symbol(props, Decl(destructuringInitializerContextualTypeFromContext.ts, 16, 21))

// Repro from #29189

declare function f(g: (as: string[]) => void): void
>f : Symbol(f, Decl(destructuringInitializerContextualTypeFromContext.ts, 18, 54))
>g : Symbol(g, Decl(destructuringInitializerContextualTypeFromContext.ts, 22, 19))
>as : Symbol(as, Decl(destructuringInitializerContextualTypeFromContext.ts, 22, 23))

f(([_1, _2 = undefined]) => undefined)
>f : Symbol(f, Decl(destructuringInitializerContextualTypeFromContext.ts, 18, 54))
>_1 : Symbol(_1, Decl(destructuringInitializerContextualTypeFromContext.ts, 23, 4))
>_2 : Symbol(_2, Decl(destructuringInitializerContextualTypeFromContext.ts, 23, 7))
>undefined : Symbol(undefined)
>undefined : Symbol(undefined)

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,24 @@ const Child: SFC<Props> = ({
>`name: ${name} props: ${JSON.stringify(props)}` : string
>name : "Apollo" | "Artemis" | "Dionysus" | "Persephone"
>JSON.stringify(props) : string
>JSON.stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
>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; }
>JSON : JSON
>stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (string | number)[], space?: string | number): string; }
>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; }
>props : {}

// Repro from #29189

declare function f(g: (as: string[]) => void): void
>f : (g: (as: string[]) => void) => void
>g : (as: string[]) => void
>as : string[]

f(([_1, _2 = undefined]) => undefined)
>f(([_1, _2 = undefined]) => undefined) : void
>f : (g: (as: string[]) => void) => void
>([_1, _2 = undefined]) => undefined : ([_1, _2]: string[]) => undefined
>_1 : string
>_2 : string | undefined
>undefined : undefined
>undefined : undefined

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @strict: true

interface SFC<P = {}> {
(props: P & { children?: any }): any | null;
}
Expand All @@ -16,4 +18,9 @@ const Child: SFC<Props> = ({
children,
name = "Artemis",
...props
}) => `name: ${name} props: ${JSON.stringify(props)}`;
}) => `name: ${name} props: ${JSON.stringify(props)}`;

// Repro from #29189

declare function f(g: (as: string[]) => void): void
f(([_1, _2 = undefined]) => undefined)