Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 20, 2025

This PR ports the changes from microsoft/TypeScript#62604, which implements proper propagation of the VarianceFlags.Unreliable flag during type argument comparison.

Background

The TypeScript compiler computes variance information for generic type parameters to determine how types with different type arguments relate to each other. This variance can be:

  • Covariant: T<A> is assignable to T<B> if A is assignable to B
  • Contravariant: T<A> is assignable to T<B> if B is assignable to A
  • Bivariant: Both relationships hold
  • Invariant: T<A> is only assignable to T<B> if A and B are identical

Additionally, variance can be marked as Unreliable when the computation involves complex type relationships that may not provide fully trustworthy variance information.

The Problem

Previously, the Unreliable flag computed during variance analysis was not being propagated when comparing type arguments. This could lead to incorrect type checking in edge cases involving complex generic types with mapped types, conditional types, or intersection types.

The Fix

Modified the typeArgumentsRelatedTo function in internal/checker/relater.go to propagate the Unreliable variance flag:

} else {
    // Propagate unreliable variance flag
    if r.c.inVarianceComputation && varianceFlags&VarianceFlagsUnreliable != 0 {
        r.c.instantiateType(s, r.c.reportUnreliableMapper)
    }
    // ... rest of variance checking
}

When we're computing variance and encounter a type parameter with the Unreliable flag, we now instantiate the source type with reportUnreliableMapper to properly track and propagate this reliability information through subsequent type comparisons.

Testing

All existing tests continue to pass. The original TypeScript PR includes a new test case (variancePropagation.ts), which will be automatically included once the submodule is updated after the upstream PR is merged.

Related

Note: The upstream PR is currently open and not yet merged. This port implements the final version of the change (which propagates only Unreliable, not Unmeasurable, to avoid performance impacts).

Original prompt

Port microsoft TypeScript pull 62604

Note

Custom agent used: Strada to Corsa Port Expert
A Go and TypeScript expert who can easily figure out how to port PRs from one language to another


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Port Microsoft TypeScript pull request 62604 Port TypeScript PR #62604: Propagate variance reliability Oct 20, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh October 20, 2025 22:25
Copilot finished work on behalf of RyanCavanaugh October 20, 2025 22:25
@jakebailey jakebailey marked this pull request as ready for review October 20, 2025 22:56
@Copilot Copilot AI review requested due to automatic review settings October 20, 2025 22:56
Copy link
Contributor

@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 ports TypeScript PR #62604, which fixes variance reliability propagation in type argument comparison. The change ensures that when computing variance, the Unreliable flag is properly tracked through the type system to avoid incorrect type checking in complex generic scenarios.

Key Changes:

  • Modified typeArgumentsRelatedTo function to propagate the VarianceFlags.Unreliable flag during variance computation
  • Restructured conditional logic to check for unreliable variance before performing covariant/contravariant/bivariant checks

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

LGTM though did it did not include the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants