microsoft / microsoft/TypeScript
Assignability between distributive conditional types and their branch type is reversed in contravariant positions
@ahejlsberg 已经在做这个了。
开始于 2026年7月30日。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 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_
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。