Repository navigation
Correlated type constraint breaks under return type inference #32804
Description
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:
A different, more complex example,
#14829 (comment)
[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 =(
Activity
[-]Correlated type constraint breaks [/-][+]Correlated type constraint breaks under return type inference if function param type is also inferred[/+][-]Correlated type constraint breaks under return type inference if function param type is also inferred[/-][+]Correlated type constraint breaks under return type inference[/+]AnyhowStep commentedon Aug 11, 2019
From the behaviour of the above snippets and the inferred types displayed by the tooltips, it just feels like someone forgot to copy-paste some type inference logic or something.
Like, the
Inferred type, foo<XStr, 1>snippet's type is inferred asfoo<XStr, 1>but it behaves likefoo<U, 1>AnyhowStep commentedon Aug 11, 2019
!@#$ YES!
I HAVE FINALLY FIGURED OUT A WORKAROUND!
Playground
The magic is here,
It turns
ReturnTinto a union of tuples (2-tuples, in this case), and then uses rest parameters[Edit]
After spending a good chunk of my Saturday trying to think of a workaround, I suddenly came up with this idea after randomly remembering my comment here,
#32596 (comment)
I had brought up using tuples and rest parameters as a possible workaround for that issue (there was a different, better way to express it in that issue, though)
[Edit]
Wait... I just realized that workaround doesn't useValueT. I must be seriously tired to be making silly mistakes.Brb. Gonna' fix that up. (Or try).My use case requires both
ReturnTandValueTand does magic with it in the return type.[Edit]
Fixed it to use
ValueT[Edit]
Nevermind. I tried to adapt it to fit my actual use case and this workaround broke apart.
For one, I want to give my
ReturnTthe option to be a union type.So, I can't have the conditional type perform distribution.
If it doesn't distribute, we go back to square 1 with the inference problems.
Playground
AnyhowStep commentedon Aug 11, 2019
This is closer to my personal use-case,
Playground
I need to allow a union type to be returned and handle that accordingly.
My actual use case needs to use
UnionToIntersection<>, though.AnyhowStep commentedon Aug 11, 2019
Seems like using
UnionToIntersection<>breaks it,NoInfer<>doesn't help, either.Playground
Better repro of how
UnionToIntersection<>wrecks this workaround,Playground
RyanCavanaugh commentedon Aug 16, 2019
I'm ignoring the long posts other than the OP 😐
Why isn't the function definition just this?
Alternatively it seems like you need a non-inferential type parameter usage operator? #14829
AnyhowStep commentedon Aug 16, 2019
I need
argto possibly be a subtype ofReturnT["x"]It's used for the return type in my actual use case. (The return type was
voidin my OP to simplify)All attempts at using the workarounds for non inferential type param usage break for this use case, at the moment =/
The function represents a column in SQL. The return type is the type of the column.
The
ValueTrepresents a possible value of the column.Let's say we have this query being built by the library,
Let's say the type of the column is
null|number.If we add this
WHEREclause,Then we can statically narrow the type of
myColumnin the query to1, without even executing the query.