microsoft / microsoft/TypeScript

Type inference inconsistency with generic constraints

オープン
#43,804 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground でジェネリック制約の例を指定された各バージョンにわたって再現し、test_4 の推論された型を確認してください。concat("foo", "bar") がエラーなしで string を推論し、他の例を維持したまま期待される動作と一致すれば、修正は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。