microsoft / microsoft/TypeScript
Object with all context-sensitive properties requires at least one non-context-sensitive property for inference to work
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 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
💻 Code
// 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:
<D extends {
initial: keyof D["states"],
context: object,
states: {
[S in keyof D["states"]]: {
entry?: (context: D["context"], state: S) => void,
_?: null
}
}
}>
(definition: IdentityObject<D>) =>
D
type Identity<T> =
T extends any
? ( T extends (...a: never) => unknown ? T :
T extends object ? IdentityObject<T> :
T
)
: never
type IdentityObject<T> =
{ [K in keyof T]: Identity<T[K]> }
🙁 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンク先の TypeScript Playground で2つの createMachine の例を使い、推論の違いを再現します。issue にあるジェネリック宣言と IdentityObject 型を読み、その後、コンテキスト依存の entry プロパティに対するコンパイラの型パラメータ推論を追跡します。追加の _ プロパティがないバージョンで、context が { foo: number } として、state が "a" として推論されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100