-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Don't try to add an implicit undefined for mapped properties outside of strictNullChecks
#60393
base: main
Are you sure you want to change the base?
Don't try to add an implicit undefined for mapped properties outside of strictNullChecks
#60393
Conversation
…of `strictNullChecks`
@@ -814,7 +814,7 @@ export function createSyntacticTypeNodeBuilder( | |||
} | |||
function typeFromProperty(node: PropertyDeclaration | PropertySignature | JSDocPropertyTag, symbol: Symbol, context: SyntacticTypeNodeBuilderContext) { | |||
const declaredType = getEffectiveTypeAnnotationNode(node); | |||
const requiresAddingUndefined = resolver.requiresAddingImplicitUndefined(node, symbol, context.enclosingDeclaration); | |||
const requiresAddingUndefined = strictNullChecks && resolver.requiresAddingImplicitUndefined(node, symbol, context.enclosingDeclaration); |
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 have a bad feeling that making isolated declaration emit more dependent on compiler options is a bad idea... Theoretically adding undefined here isn't a big deal because it "doesn't exist" when handled by the checker.
How does this end up fixing the assert?
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.
Hm, this isn't that relevant to ID (from what I understand). In the example, we want to generate types for an output of a factory function and ID requires explicit annotations - so it wouldn't even run into this problem.
The issue arises because you have a homomorphic mapped type (that preserves modifiers) running over prop?: undefined
. So it ran into this code in requiresAddingImplicitUndefined
:
switch (declaration.kind) {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.JSDocPropertyTag:
symbol ??= getSymbolOfDeclaration(declaration);
const type = getTypeOfSymbol(symbol);
return !!(symbol.flags & SymbolFlags.Property && symbol.flags & SymbolFlags.Optional && isOptionalDeclaration(declaration) && (symbol as MappedSymbol).links?.mappedType && containsNonMissingUndefinedType(type));
And then that led to obtaining an optional type for undefined
(that's the type
in the code above) but we never should try to do that with strictNullChecks: false
. And in a sense, this proposed fix is solely based on that fact - we don't ever have to "add" undefined
to anything when strictNullChecks: false
.
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 could also fix this by avoiding adding undefined
to... undefined
but this seemed cleaner and it potentially skips some extra redundant work (although I perhaps could try to detect this in requiresAddingImplicitUndefined
too)
fixes #60390