Skip to content

Remove undefined from the type of default initialised parameters when narrowing #14498

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 6 commits into from
Mar 7, 2017

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Mar 7, 2017

When narrowing, remove optionality from the initial type of parameters with initialisers. Note that the type of the initialiser is not used; its presence just means that the initial type of the parameter
can't contain undefined. It could contain any other member of a declared union type.

This fix builds on the previous fix #12033, but is slightly more complex in that it changes the initial type before control flow analysis rather than just throwing undefined away from the declared type.

The code refactors most of the complexity into checkIdentifier, out of getFlowTypeOfReference, since it's only needed there. Suggestions on simplifying the mess of conditionals are welcome!

Fixes #14487
Fixes #14425
Fixes #14236

sandersn added 2 commits March 6, 2017 16:21
When narrowing, remove optionality from the initial type of
parameters with initialisers. Note that the type of the initialiser is
not used; its presence just means that the initial type of the parameter
can't contain undefined. It could contain any other member of a declared
union type.
@sandersn
Copy link
Member Author

sandersn commented Mar 7, 2017

@ahejlsberg can you take a look?

@Jessidhia
Copy link

Jessidhia commented Mar 7, 2017

That is, it's treated as a narrowing instead of removing |undefined from the type?

declaration.initializer &&
getFalsyFlags(declaredType) & TypeFlags.Undefined &&
!(getFalsyFlags(checkExpression(declaration.initializer)) & TypeFlags.Undefined);
return annotationIncludesUndefined ? getNonNullableType(declaredType) : declaredType;
Copy link
Member

Choose a reason for hiding this comment

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

This line is the source if the issue with null I mentioned. It removes both undefined and null. You need to use getTypeWithFacts(declaredType, TypeFacts.NEUndefined) instead.

Copy link
Member

Choose a reason for hiding this comment

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

This is the cause of #14236.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) :
type === autoType || type === autoArrayType ? undefinedType :
includeFalsyTypes(type, TypeFlags.Undefined);
const flowType = isFlowNarrowable(node, type, !assumeInitialized) ? getFlowTypeOfReference(node, type, initialType, flowContainer) : type;
Copy link
Member

Choose a reason for hiding this comment

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

I would keep the isFlowNarrowable logic inside getFlowTypeOfReference, even if it means adding another parameter. Otherwise, you have to always write this combined pattern which is even more complicated.

let key: string;
if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
if (!isFlowNarrowable(reference, declaredType, couldBeUninitialized)) {
Copy link
Member

Choose a reason for hiding this comment

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

Just inline the function here. It's not called anywhere else best I can tell.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@sandersn sandersn merged commit 1bf4f06 into master Mar 7, 2017
@sandersn sandersn deleted the narrow-default-initialised-parameters branch March 7, 2017 23:04
@sandersn sandersn mentioned this pull request Jan 22, 2018
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants