microsoft / microsoft/TypeScript
Object properties are inferred in the wrong order: should infer properties with `NoInfer<T>` AFTER properties with `T`
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
🔎 Search Terms
NoInfer
brittle inference
object properties
Related issues:
- https://github.com/microsoft/TypeScript/issues/56297 : inference depends on the order of the properties. This explains the inconsistency in my issue.
🕗 Version & Regression Information
Tested in 6.0.2 and nightly.
⏯ Playground Link
💻 Code
See playground for the full example.
type fooArgs<T> = {
a: (_: A) => X<T>,
// note: NoInfer<Y<T>> better than Y<NoInfer<T>>, doesn't change this issue though
b: NoInfer<Y<T>>
}
function foo<T>(args: fooArgs<T>) {}
foo({
a: (_) => ...,
b: ... // inference of the value given to b is VERY brittle.
});
🙁 Actual behavior
Sometimes, b is inferred before a.
Then, as a is the one used to infer the generic parameter T, the value given to b doesn't have the correct type (because inferred before a).
This is likely due to the fact that the value given to a is a callback with a parameter we didn't specify the type ((_) => ...). Therefore, TS has first to look at fooArgs<T> to infer the callback's parameter type (i.e. the type of _).
Due to that, b is sometime inferred before a has been properly inferred. This behavior depends on the order of the object properties (cf related issue). It is also influenced by other defined properties, and how they are defined. Making the inference quite chaotic.
🙂 Expected behavior
TS should infer NoInfer<T> properties AFTER T properties, in order to prevent such issues.
Additional information about the issue
The fact that the order of the inference changes is quite troublesome, hiding the issue in some cases.
Could be nice to have a kind of tool/flag to detect such kind of potential issues, and to help debugging.
EDIT: A possible workaround:
type fooArgs<T> = {
b: Y<T>
}
function foo<T>(a: (_: A) => X<T>, args: NoInfer<fooArgs<T>>) {}
foo(
a: (_) => ..., {
b: ...
});
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 Playground 和精简后的 fooArgs/foo 示例开始,改变对象属性的顺序以及 callback 参数的注解。跟踪 NoInfer 属性的推断方式,并将其行为与相关 issue #56297 进行比较。完成的标准是:该示例始终先从包含 T 的属性推断 T,再处理 NoInfer 属性,并且已解决所报告的顺序依赖行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100