microsoft / microsoft/TypeScript
this type in conditional type false clause is incorrectly typed
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
TypeScript Version: 3.7.3
Search Terms:
generic type this widening incorrect
Code
type Option1 = { run(this: { b: 1 }): void }
function f1(options: Option1) { return options }
// `this` is correctly typed as `{ b: 1 }`
f1({ run() { this.b } })
type Option2<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(arg: { b: 1 }): void
}
function f2<Config>(options: Option2<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f2({ config: { a: 1 }, run() { this.a } })
// `arg` is correctly typed as `{ b: 1 }`
f2({ run(arg) { arg.b } })
type Option3<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(this: { b: 1 }): void
}
function f3<Config>(options: Option3<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f3({ config: { a: 1 }, run() { this.a } })
// `this` is widen to what `Config` extends to. In this case `Record<string, any>`
f3({ run() { this.b } })
Expected behavior:
this in f3({ run() { this.b } }) should be typed as in this in f1() and arg in f2()
Actual behavior:
this in f3({ run() { this.b } }) is typed as the base type of Config (Record<string,any> in the example above)
Playground Link:
Playground Link
Related Issues:
Some maybe related issues:
https://github.com/microsoft/TypeScript/issues/32990
https://github.com/microsoft/TypeScript/issues/30152
https://github.com/microsoft/TypeScript/issues/13995
I also recall there were some discussion about type and Record<>, but that is in the gitter channel and I can't find relevant issue or doc about it.
I think this is different than the related issues above because this does not involve union type.
This is about the false condition of the conditional type Config extends Record<string, any> ? ... : ... do not use the generic type Config and should not be affected by it.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
重现链接的 TypeScript Playground 中的示例,比较 f1、f2 和 f3 中 this 的上下文类型。调查 false 子句的条件类型上下文类型推断;当 f3 回调的 this 被推断为 { b: 1 } 而不是扩大的基类型,且其他示例没有发生回归时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100