microsoft / microsoft/TypeScript
Covariant generic parameters fail unification
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 Search Terms
"contravariant", "unification"
### 🕗 Version & Regression Information
- This changed between versions 3.3.3 and 3.5.1
### ⏯ Playground Link
https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBKZQE4EMBuzEEtkJgXhgAoBKfAPhgG8AoGOcaegWwCNMwBTfGAHgGEoADzKEA+gEYAXEWCSBg0ngpgArgBs1AGhiiATNMKz5i5erXEDJmPPJErKsABMOAM3YdHMZBC9gAngDc1LT0kLBqHBAQovDQWJyeBPwwHIJQHE4+lBFgAOZQABaSqqwciAC+IrLWFjCqGrb1akF0sbBMIIgcMQxI7om8fClpGY5ZOflFJSxl2sAFGHwgzgCCUGLFKqWIFtNllYbSfLVNjWYtoYzp0OLcoKzuhBFRPWHxHtodXa9x-cQXbRg1ygujuIAenEIX26bXejm0z2isL+AN6MEQ3GB4hIQXKwUBoHQWBwsAIJFsNFaaPubE43H4QhEEkstnk2j0LKU1iEtRsXIczjcCS8PhwgWCVLCMERPz6wqSQ1S6UyVAmhU22wOvNsAtc-RFvnFksY0NlcPpipGKuyGUmGpmiDmC0QS1W61E9rKuy2Dq1Rx1Tj1wu8htRUqxYIhHCekSRvThn06MPjKJCgOBoIINMepuRCQRsbNqfKQA
### 💻 Code
```ts
const contravariant = () => {
const combine = (_1: (c:Ctx) => null, _2: (c:Ctx) => null): () => Ctx => () => undefined as any;
const less_constrined = (c: C): null => null;
const more_constrined = (c: C): null => null;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}
const covariant = () => {
const combine = (_1: () => Ctx, _2: () => Ctx): Ctx => undefined as any;
const less_constrined = (): C => undefined as any;
const more_constrined = (): C => undefined as any;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}
```
### 🙁 Actual behavior
In the `contravariant` example the type is inferred from the first argument, so `test1` fails to typecheck, despite both `test1` and `test2` sharing the same argument types, just reversed. This is also different from the `covariant` case where types are properly unified in both `test1` and `test2`
### 🙂 Expected behavior
The behaviour for the covariant and contravariant case should match, so `test1` should be inferred as
`
() => C
`
### Additional information about the issue
_No response_
贡献指南
调研方向
从链接的 TypeScript Playground repro 开始,将逆变调用与协变调用进行比较,包括 3.3.3 和 3.5.1 版本之间报告的行为。当两种参数顺序都能一致地合一,并且第一个逆变调用推断出预期行为中所示的约束更严格的 C 类型时,该 issue 即完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100