-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
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.
@ahejlsberg can you take a look? |
That is, it's treated as a narrowing instead of removing |
src/compiler/checker.ts
Outdated
declaration.initializer && | ||
getFalsyFlags(declaredType) & TypeFlags.Undefined && | ||
!(getFalsyFlags(checkExpression(declaration.initializer)) & TypeFlags.Undefined); | ||
return annotationIncludesUndefined ? getNonNullableType(declaredType) : declaredType; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/compiler/checker.ts
Outdated
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; |
There was a problem hiding this comment.
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.
src/compiler/checker.ts
Outdated
let key: string; | ||
if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { | ||
if (!isFlowNarrowable(reference, declaredType, couldBeUninitialized)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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 ofgetFlowTypeOfReference
, since it's only needed there. Suggestions on simplifying the mess of conditionals are welcome!Fixes #14487
Fixes #14425
Fixes #14236