microsoft / microsoft/TypeScript

Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work

未关闭
#64,251 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

object with all context-sensitive properties, self referencial types, xstate

### 🕗 Version & Regression Information

Tested with TypeScript 7.0.2, the issue exists even in versions as old as 4.4.4

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEAcCcHsCMBsCmBbAXKAJtRBnAdgOQAuoAlngGaKSgAioAxtJJIg0fAJ4A0j0eRRAA8SAQzwZQOIqMGhRreXk4AoBq1mIAsqIYALcogAUAbxWgyeUkVKj46AESiH3c3wHCi6E6ArRo6ACMoAC+rhbSmjjebhaiMRaJoIgCkJzoRkweIryRggCUoAC8AHygZklJWYIisZUgoAB6APx1SXmIbYkNLV0hdf0W-SH5KioNONDwAK42-OiiGJKioNM4iEg4OKB4-AC01Z5763g41qQAbogQMODURJygAAYA+uh40-DwT6BqGoI6fSGUxucjnOyOZzhdw1LzlXz+IKhaEdaLlOrxdGVZKpdKgTL8WG5GQFYplCrYmGeLoWHqtSlSEmdBl0vrQpJvHafeADNzDUYqDBseAKa5ZaSMf7aXQGPCIVBuAA89E8KQw2wpFjBNghoAA1ohONAKHQANoOVEOAC67KpInQcAAVmwiLbUQlKqaAMqWfWG41mi1MnDWq0e7EpIhpZoZQ72wNxojW4madBewqlUAXaCkDC2jkxrlfPq8oYlOpGIUUbWkeagACSQoE1k4AHlYM72MqShny9jaGMHncG02bA9FQAVMpFNwT5IiNXbcSqJLNfGgOeqiTbIwAOn3mLlV0gvdWeD1uwA7nhQGu5wqGZuF9vQE6XbeR5GW+3O0RJ2UH0pCculGJJ3kQY9B04YdGy-B4fxdf9ijcHxTQAaV9A0jRNCcw0-ZtxwndCrTKfogAOfEUiAA

### 💻 Code

```ts
// problem: doesn't infer D correctly, context and state are any
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? any
state
// ^? any
}
}
}
})

// solution: add a useless non-context-sensitive property `_: null`
createMachine({
initial: "a",
context: { foo: 1 },
states: {
a: {
entry: (context, state) => {
context
// ^? { foo: number }
state
// ^? "a"
},
_: null
}
}
})

declare const createMachine:
void,
_?: null
}
}
}>
(definition: IdentityObject) =>
D

type Identity =
T extends any
? ( T extends (...a: never) => unknown ? T :
T extends object ? IdentityObject :
T
)
: never

type IdentityObject =
{ [K in keyof T]: Identity }
```

### 🙁 Actual behavior

The first `createMachine` invocation doesn't infer the type parameter correctly (it requires at least one non-context-sensitive property) and hence `context` and `state` parameters of `entry` are not inferred.

### 🙂 Expected behavior

The first `createMachine` invocation should infer the type parameter correctly without having to pass in a non-context-sensitive property leading to inference of `context` and `state` parameters of `entry`

### Additional information about the issue

The premise is adding (or rather not adding) an extra useless property shouldn't make a difference to the inference

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,使用两个 createMachine 示例,在链接的 TypeScript Playground 中复现推断差异。阅读 issue 中的泛型声明和 IdentityObject 类型,然后跟踪编译器针对依赖上下文的 entry 属性进行类型参数推断的过程。当不带额外 _ 属性的版本将 context 推断为 { foo: number },并将 state 推断为 "a" 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。