microsoft / microsoft/TypeScript

Assignability between distributive conditional types and their branch type is reversed in contravariant positions

Open
#63,694 0 comments 0 reactions 1 assignee Claimed by @ahejlsberg View on GitHub
Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

"conditional type distribution contravariance" "distributive conditional assignability" "variance conditional types"

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types, distributive conditional types, and variance

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAwg9gNwDwBUB8UC8UDeUBOEAhgCZwB2ANiFAB4BcUKUAvgNwBQokUAIgJYBnYPnjJ0WJlAi1gEciUFSA-LESoMjchAQR8nDgDMAruQDGwfhShzhYjQAoijAcNHr0AGigAjRvfQASlwOKCgAenCoAHkAaX91AEEMISgiQUF+AHNyIh9KaGA4NWREqAAfKAAhNFC0qEEiS0FDfgglALQDMMioAAFgQQBaGUgLEfx8OHwSpDLKmqhU8jhgNIzs3PzC4vtkup8GpqFW9r4hEU7OFg4ucGh4chEiDUkHBiZgzAwEOH4STjcaCuS4UZ6vbDMGRyBRKZiqR7giRaHR6AwmcyWay2YCI-AvdBOFwXdxPfEabx+NRkgloYI4Oq9AbDUYQcZ6KYzPEvZINAAWcGMlBIUBWax80HSmRyeQKNl2YPJ82qtTCRCOzVOHUVtO6ESi3LmFRV-MFwt8ko2Mu28up4P2YUOjU1bSUINJSK6HBYQA

### 💻 Code

```ts
type Cov = { readonly x: T };
type DistrCov = T extends T ? Cov : never;

function testCov(a: DistrCov, b: Cov) {
// OK: Cov is assignable to Cov
a satisfies Cov;

// @ts-expect-error Cov is not assignable to Cov
b satisfies DistrCov;
}

type Contra = (x: T) => void;
type DistrContra = T extends T ? Contra : never;

function testContra(a: DistrContra, b: Contra) {
// @ts-expect-error Contra
should not be assignable to Contra
a satisfies Contra;

// Contra should be assignable to Contra
b satisfies DistrContra;
}
```

### 🙁 Actual behavior

The covariant case behaves as expected.

The contravariant case appears inverted:

- `DistrContra` is accepted as `Contra`, although it should not be.
- `Contra` is rejected as `DistrContra`, although it should be.

### 🙂 Expected behavior

The contravariant case should mirror the covariant one with assignability reversed.

Since conditional distribution over a union produces `Contra | Contra | ...`, it should only be assignable in the contravariant direction.

### Additional information about the issue

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.