Repository navigation
Remove undefined from the type of default initialised parameters when narrowing - #14498
Nathan Shively-Sanders (sandersn) merged 6 commits into
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.
|
Anders Hejlsberg (@ahejlsberg) can you take a look? |
|
That is, it's treated as a narrowing instead of removing |
| declaration.initializer && | ||
| getFalsyFlags(declaredType) & TypeFlags.Undefined && | ||
| !(getFalsyFlags(checkExpression(declaration.initializer)) & TypeFlags.Undefined); | ||
| return annotationIncludesUndefined ? getNonNullableType(declaredType) : declaredType; |
There was a problem hiding this comment.
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.
This is the cause of #14236.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
| function getFlowTypeOfReference(reference: Node, declaredType: Type, initialType = declaredType, flowContainer?: Node, couldBeUninitialized?: boolean) { | ||
| let key: string; | ||
| if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { | ||
| if (!isFlowNarrowable(reference, declaredType, couldBeUninitialized)) { |
There was a problem hiding this comment.
Just inline the function here. It's not called anywhere else best I can tell.
There was a problem hiding this comment.
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