Don't obtain apparent type of contextual types being optional type variables#61635
Conversation
There was a problem hiding this comment.
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))) { |
There was a problem hiding this comment.
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.
- 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