microsoft / microsoft/TypeScript
Correlated type constraint breaks under return type inference
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version: 3.5.1
Search Terms:
return type, generic, constraint, assignable, correlated type
Code
type XStr = {x:string};
type XNum = {x:number};
type U = XStr|XNum;
type Args = { str : XStr, num : XNum };
declare function foo<
ReturnT extends U,
ValueT extends ReturnT["x"]
> (
f : (args : Args) => ReturnT,
value : ValueT
) : void;
/*
Error as expected.
Type 'string | number' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.
*/
foo<XStr, string|number>(
(args:Args) => args.str,
""
);
//Inferred type, foo<XStr, string | number>
foo(
args => args.str,
//Expected: Error
//Actual: OK
"" as string|number
);
//Inferred type, foo<XStr, string>
foo(
//Added explicit type annotation to function params
(args:Args) => args.str,
/*
Error as expected.
Type 'string | number' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.
*/
"" as string|number
);
/////
/*
Error as expected.
Type '1' does not satisfy the constraint 'string'.
*/
foo<XStr, 1>(
(args:Args) => args.str,
1
);
//Inferred type, foo<XStr, 1>
foo(
args => args.str,
//Expected: Error
//Actual: OK
1
);
//Inferred type, foo<XStr, string>
foo(
//Added explicit type annotation to function params
(args:Args) => args.str,
/*
Error as expected.
Type '1' does not satisfy the constraint 'string'.
*/
1
);
Expected behavior:
I'm just calling it a correlated type because it reminds me of correlated subqueries from SQL.
- The constraint type of
ValueTis dependent on the type ofReturnT. - When
fdoes not have parameters, or all parameters are explicitly annotated,
ValueTis inferred correctly. - When
fhas parameters that are not explicitly annotated,
ValueTis inferred incorrectly. - Attempting to explicitly set invalid type paramters will error as expected.
foo<XStr, string|number>should not be allowedfoo<XStr, 1>should not be allowed
Actual behavior:
foo<XStr, string|number>is allowed under inferencefoo<XStr, 1>is allowed under inference
Playground Link:
Related Issues:
https://github.com/microsoft/TypeScript/issues/32540#issuecomment-520193240
https://github.com/microsoft/TypeScript/issues/29133
A different, more complex example,
https://github.com/microsoft/TypeScript/issues/14829#issuecomment-520191642
[Edit]
Can someone come up with a better name for this?
I'm working on rewriting my type-safe SQL builder library and it relies on the return type of generic functions being inferred correctly. But it seems like return type inference just breaks in so many unexpected ways.
Anonymous callback functions are used a lot for building the WHERE, ORDER BY, GROUP BY, HAVING, JOIN, etc. clauses.
Since return type inference for generic functions is not robust, it's basically a blocker for me =(
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue にある TypeScript Playground の再現から始め、関連する issue 32540、29133、14829 と比較してください。ジェネリックな戻り値の型と制約の推論を追跡します。示されたケースで、推論された呼び出しが string|number と 1 を拒否しつつ、有効な string の推論は引き続き受け入れられれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100