microsoft / microsoft/TypeScript
Type inference inconsistency with generic constraints
オープン
まだ誰も着手していません。
Needs Investigation
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
🔎 Search Terms
- type inference const string
- type inference generic constraints
🕗 Version & Regression Information
Tested on
- 3.9.7
- 4.2.3
- 4.3.0-beta
⏯ Playground Link
💻 Code
Following Generic Constraints
type Concatable<T> = {
concat: (...xss: T[]) => T
};
export const concat = <T extends Concatable<T>>
(xs: T, ...xss: T[] ): T => xs.concat( ...xss );
export const test_1 = concat([1, 2], [3, 4]); // OK
export const test_2 = concat("foo" as string, "bar"); // OK
export const test_3 = concat<string>("foo", "bar"); // OK
// Expected: 'test_4: string'
// Got: 'test_4: Concatable<"foo" | "bar">'
export const test_4 = concat("foo", "bar");
// Error: ^^^^^
// Argument of type 'string' is not assignable to
// parameter of type 'Concatable<"foo" | "bar">'
🙁 Actual behavior
test_4: Concatable<"foo" | "bar">
TS leaks the constraint for type T into type T
TS provides no good way to make Rust-like trait
🙂 Expected behavior
T, test_4, "foo" and "bar" all infer type string
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground でジェネリック制約の例を指定された各バージョンにわたって再現し、test_4 の推論された型を確認してください。concat("foo", "bar") がエラーなしで string を推論し、他の例を維持したまま期待される動作と一致すれば、修正は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100