microsoft / microsoft/TypeScript
Generics return type of function reported not assignable.
オープン
まだ誰も着手していません。
Bug
Domain: check: Type Inference
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
🔎 Search Terms
return value, typescript, generics
🕗 Version & Regression Information
This changed between versions 3.5.1 and 3.6.3
⏯ Playground Link
💻 Code
type TypeCaseA<A> = { data: A };
type TypeCaseB<B = string> = { data?: B; code: string; };
type TypeCaseAB<A, B = string> = TypeCaseA<A> | TypeCaseB<B>;
class Service {
getB<B = string>(code: string, data?: B): TypeCaseB<B> {
const result: TypeCaseB<B> = { data, code };
return result;
}
getA<A>(data: A): TypeCaseA<A> {
return { data };
}
func1(): TypeCaseAB<{ id: number }> {
/* The following line is blamed with:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
*/
return this.getB('1231');
}
func2(): TypeCaseAB<{ id: number }> {
const retval = this.getB('1231');
return retval;
}
func3(): TypeCaseAB<{ id: number }, string> {
return this.getB('1231', '456');
}
}
🙁 Actual behavior
func1 is tested with the following error:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
There should be no difference from func2 which is tested correct.
🙂 Expected behavior
No error reported.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground で、TypeCaseA、TypeCaseB、TypeCaseAB、および Service.func1/func2/func3 の例を使用して診断を再現する。func1 におけるジェネリックな戻り値型の処理を、受け入れられる func2 および func3 のケースと比較する。func1 がエラーを生成せず、期待される戻り値型 TypeCaseAB を保持すれば完了とする。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100