microsoft / microsoft/TypeScript
Intersected type variable with a recursive constraint fails to infer
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
intersection type variable parameter inference recursive constraint
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type StateConfig<TAction extends string> = {
entry?: TAction
states?: Record<string, StateConfig<TAction>>;
};
type StateSchema = {
states?: Record<string, StateSchema>;
};
declare function createMachine<
TAction extends string,
TConfig extends StateSchema,
>(config: TConfig & StateConfig<TAction>): [TAction, TConfig];
const inferredParams = createMachine({
entry: "foo",
states: {
a: {
entry: "bar",
},
},
});
inferredParams; // ["foo" | "bar", StateSchema]
🙁 Actual behavior
TConfig gets inferred as its constraint
🙂 Expected behavior
I'd expect TConfig to infer successfully
Additional information about the issue
This is very related to the other issue I just created (see here). The underlying problem of this particular example is the same. The inferredType for TConfig fails the compareTypes check in getInferredType here. This time there is no excess property errors apparent to the user because the excess properties are satisfied through the intersection.
Despite the problem having the same root cause I think it's worth discussing this separately. The other issue could be closed as design limitation/working as intended but this one feels to me a lot closer to being an actual bug that would be worth fixing.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された TypeScript Playground リンクから推論結果を再現し、その後 src/compiler/checker.ts の getInferredType 周辺と、その compareTypes チェックを読みます。交差された再帰的制約によって TConfig が StateSchema にフォールバックする理由を追跡します。関連する推論動作を後退させることなく、例で構成を正常に推論できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100