Skip to content

Don't obtain apparent type of contextual types being optional type variables #61635

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Conversation

Andarist
Copy link
Contributor

fixes #61633

@Copilot Copilot AI review requested due to automatic review settings April 30, 2025 21:50
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Apr 30, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Apr 30, 2025
Copy link

@Copilot Copilot AI left a 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

@@ -32412,7 +32412,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getContextualTypeForObjectLiteralMethod(node, contextFlags) :
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))) {
Copy link
Preview

Copilot AI Apr 30, 2025

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.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Without this instantiateTypeWithSingleGenericCallSignature computed contextualType as AnyConstructor | undefined
  2. then eliminated | undefined with getNonNullableType
  3. and obtained a generic contextualSignature from that
  4. and returned anyFunctionType because this happened with CheckMode.SkipGenericFunctions
  5. given anyFunctionType is non-inferrable, getInferredType picked up the default type argument (undefined)
  6. and getSignatureApplicabilityError returned with diagnostics because it was called with a check candidate that was a product of signature instantiation with currently inferred type arguments

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Broken inference for generic classes passed to a generic function taking an optional class
3 participants