Skip to content

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

Closed
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:fix/apparent-type-of-contextual-optional-type-variable
Closed

Don't obtain apparent type of contextual types being optional type variables#61635
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:fix/apparent-type-of-contextual-optional-type-variable

Conversation

@Andarist
Copy link
Copy Markdown
Contributor

fixes #61633

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
Copy Markdown
Contributor

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

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

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
Copy Markdown
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

@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

5 participants