microsoft / microsoft/TypeScript

type parameter variance in generic call signature is incorrectly bivariant

Open
#63,677 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Variance Relationships
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

unsound generic function assignability type parameter variance generic call signature bivariant

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about **generics and variance**.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAygFgewK4BsAmAhCBJAdgNwEMAnAS0N2AB4ARAPigF4oqAVOgCgA8AuKVgGQ0ANFBB9WASkZ1BNAFDyAxglwBnYFDUQVuNH3jJ0WPETIVqSXAGtcCAO64GzKN1EhJTBiGWqNUZGAAOSQAWwAjCGIDRFRMHAISckoqXDDI4mctHVU0eUCQiKiqK1sHJw4AFlEAcg1iGskAOmAEAFEuMFUISnIUDklFXX9SXEKMmKN40ySLVPSorO5PGVcuFvbO7t7CfslRLkHhzVGAVRs7R0m4k0TzFNLLpyYoUfGo+TOL8o464AbavVGoogA

### 💻 Code

```ts
type ShouldBeInvariant = (x: T&D, y: T)=>T&D

const second: ShouldBeInvariant = (x, y) => y
const outNumber: ShouldBeInvariant = second
outNumber(4, 'str').toExponential()

const inNumber: ShouldBeInvariant = (x) => (x.toExponential(), x)
const inUnknown: ShouldBeInvariant = inNumber
inUnknown('str', 'str')

```

### 🙁 Actual behavior

The code type checks, while it shouldn't because that is unsound, using `ShouldBeInvariant` covariantly or contravariantly both causes runtime errors.

### 🙂 Expected behavior

`ShouldBeInvariant` should be invariant against its parameter. Both assigns:
```ts
const outNumber: ShouldBeInvariant = second
```
and
```ts
const inUnknown: ShouldBeInvariant = inNumber
```
should report errors

### Additional information about the issue

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground example and reproduce the unsound assignments involving ShouldBeInvariant. Trace generic call-signature assignability and variance checking, then add a regression test showing that both assignments report errors and that the invalid calls no longer type-check.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.