Infer type in conditional cannot unify generics
See original GitHub issueTypeScript Version: 2.8.0-dev.20180315
Search Terms: infer conditional unify generic function
Code
type Apply1<T, V> = T extends (item: V) => infer R ? R : never;
type A1 = Apply1< (item: string) => string[] , string>;
type B1 = Apply1< <U>(item: U) => U[], string>;
type Apply2<T, V> = T extends (item: V, obj: infer R) => any ? R : never;
type A2 = Apply2< (item: string, obj: string[]) => any , string>;
type B2 = Apply2< <U>(item: U, obj: U[]) => any , string>;
type Apply3<T, V> = T extends (item: V, obj: infer R) => infer R ? R : never;
type A3 = Apply3< (item: string, obj: string[]) => string[] , string>;
type B3 = Apply3< <U>(item: U, obj: U[]) => U[] , string>;
{
"compilerOptions": {
"allowJs": true,
"target": "es6",
"module": "commonjs",
"outDir": "dest",
"strictNullChecks": true,
"jsx": "preserve",
"strictFunctionTypes": true
}
}
Expected behavior:
All A
and B
types are inferred to string[]
. For types B
, the type parameter U
should be unified with string
.
Actual behavior:
All B
types are inferred to {}[]
.
Related Issues: #22615 (different problem, but similar input)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:15
- Comments:6 (1 by maintainers)
Top Results From Across the Web
typescript: Infer generic requirement of type - Stack Overflow
The rule for when conditional types become distributive is that you are checking a "naked" or "bare" generic type parameter.
Read more >Inferring types in a conditional type - Learn TypeScript
When item1 is constructed, the condition in the conditional type is true because number[] matches (infer E)[] . E is therefore inferred to...
Read more >Lecture 8:Type Inference
The type inference problem is to take a program with no type annotations, ... It is easy to code up with heuristics for...
Read more >Documentation - TypeScript 2.4
TypeScript 2.4 introduces a few wonderful changes around the way generics are inferred. Return types as inference targets. For one, TypeScript can now...
Read more >typescript infer type - Sympa
Typescript cannot infer correct type from generic interface property. ... Conditional types in typescript allow you to introduce type variables into the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
I’ll add a simple example related to this:
Repeating my comments from #22615 as they apply equally here:
It is effectively a design limitation. We have the concept of instantiating a generic function type in the context of a non-generic function type (a form of unification), but we currently don’t do that in conditional types. Instead, function type parameters are erased to their constraints and we infer from those. In the example above, that causes us to infer
{}
for theinfer R
type parameter.The proper fix for this would be have type inference perform instantiation of a source type in the context of a target type when the source type is a generic function type.