microsoft / microsoft/TypeScript
Infer type in conditional cannot unify generics
Open
@ahejlsberg is already working on this.
Since Mar 21, 2018.
Needs Proposal
Suggestion
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript 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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.