microsoft / microsoft/TypeScript
Two way assignability condition with generic params is not working as expected
未关闭
还没有人认领这个 Issue。
Bug
Domain: Conditional Types
Needs Human Review
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
Bug Report
🔎 Search Terms
assignability, ternary, conditions, generics
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
function f<G>() {
const x: NumberIfExtends<G, G> = 1;
}
🙁 Actual behavior
The value 1 is not assignable to variable x, with the following error: Type 'number' is not assignable to type 'NumberIfExtends<G, G>'.
🙂 Expected behavior
This should be assignable, since the the value passed to both T and U generic params is the same.
Please note the following very similar examples, that works without errors.
This is to show this is a minimal reproduction, and that the different do not justify the error:
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
const x: NumberIfExtends<'some type', 'some type'> = 1; // if I pass an explicit type (Not a generic)
type NumberIfExtends<T, U> = T extends U ? number : string; // if I use the condition in only one way.
function f<G>() {
const x: NumberIfExtends<G, G> = 1;
}
function f<G>(): void {
const x: G extends G ? G extends G ? number : string : string = 1; // if I define the type without an alias. This is not always possible if instead of defining a variable I would be passing the value to a function who defines its own type
}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 TypeScript Playground 和最小的 NumberIfExtends<G, G> 复现开始。跟踪如何检查带泛型参数的条件类型的可赋值性,并比较别名形式与内联示例和单向示例。完成标准是:将 1 赋值给泛型条件类型时能够成功,且不会出现所报告的错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100