-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Don't obtain apparent type of contextual types being optional type variables #61635
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
base: main
Are you sure you want to change the base?
Don't obtain apparent type of contextual types being optional type variables #61635
Conversation
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.
Pull Request Overview
This PR addresses #61633 by fixing an incorrect treatment of contextual types that are optional type variables. The changes include updating the type checker logic to use a non‑nullable version of the instantiated type when verifying type variable status, and adding corresponding tests.
Reviewed Changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/cases/compiler/inferenceFromGenericClass1.ts | Adds tests to verify proper inference when using generic classes with optional type variables. |
| tests/baselines/reference/inferenceFromGenericClass1.js | Updates baseline output corresponding to the new test behavior. |
| src/compiler/checker.ts | Adjusts the type checking logic to use getNonNullableType and maybeTypeOfKind in the constraint check. |
Files not reviewed (2)
- tests/baselines/reference/inferenceFromGenericClass1.symbols: Language not supported
- tests/baselines/reference/inferenceFromGenericClass1.types: Language not supported
| getContextualType(node, contextFlags); | ||
| const instantiatedType = instantiateContextualType(contextualType, node, contextFlags); | ||
| if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) { | ||
| if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && maybeTypeOfKind(getNonNullableType(instantiatedType), TypeFlags.TypeVariable))) { |
Copilot
AI
Apr 30, 2025
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 change replaces the direct check on instantiatedType.flags with a call to maybeTypeOfKind on a non-nullable version of instantiatedType. Please double-check that using getNonNullableType ensures that optional type variables are not incorrectly inferred as type variables.
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.
- Without this
instantiateTypeWithSingleGenericCallSignaturecomputedcontextualTypeasAnyConstructor | undefined - then eliminated
| undefinedwithgetNonNullableType - and obtained a generic
contextualSignaturefrom that - and returned
anyFunctionTypebecause this happened withCheckMode.SkipGenericFunctions - given
anyFunctionTypeis non-inferrable,getInferredTypepicked up the default type argument (undefined) - and
getSignatureApplicabilityErrorreturned with diagnostics because it was called with a check candidate that was a product of signature instantiation with currently inferred type arguments
f29693d to
529fc9e
Compare
fixes #61633