microsoft / microsoft/TypeScript

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

Aperta
#63,694 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@ahejlsberg ci sta già lavorando.

Dal 30/7/2026.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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_

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.