microsoft / microsoft/TypeScript

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

Đang mở
#63,694 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

@ahejlsberg đang làm issue này rồi.

Từ ngày 30/7/2026.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.