Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Nov 2, 2024

fixes #60390

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Nov 2, 2024
@@ -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);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

5.6.3 tsc crash: Error: Debug Failure. False expression. at getOptionalType
4 participants