microsoft / microsoft/TypeScript
Correlated type constraint breaks under return type inference
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
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 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中的 TypeScript Playground 复现开始,并与相关 issue 32540、29133 和 14829 进行比较。跟踪泛型返回类型和约束的推断;当推断出的调用在所示情况下拒绝 string|number 和 1,同时仍接受有效的 string 推断时,即可完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100