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

Fix initializer widening #29192

merged 4 commits into from
Dec 29, 2018

Conversation

ahejlsberg
Copy link
Member

Fixes #29189.

@ahejlsberg ahejlsberg requested a review from weswigham December 28, 2018 19:41
@weswigham
Copy link
Member

weswigham commented Dec 28, 2018

You didn't add a (full) test for #29189 - you set strict: true, but the report is also for non-strict mode (as far as I can tell). And the behavior is very different here, so it matters.

@@ -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

@ahejlsberg
Copy link
Member Author

You didn't add a (full) test for #29189 - you set strict: true, but the report is also for non-strict mode (as far as I can tell). And the behavior is very different here, so it matters.

I added --strict because the issue only shows up with strict null checks enabled.

@ahejlsberg ahejlsberg merged commit 8e14031 into master Dec 29, 2018
@ahejlsberg ahejlsberg deleted the initializerWidening branch December 29, 2018 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants