microsoft / microsoft/TypeScript
Allow use of infer in extends clause of generic type parameter
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
generic type parameter infer keyword conditional
Suggestion
Allow the use infer keyword in the extends clause of generic type parameters. This would simply be a nice quality of life improvement to avoid unnecessary conditional types or extra free type parameters.
type ArrayType<A extends Array<infer T>> = T;
Use Cases
Currently if you have a generic type whose parameter is constrained by a generic type, you need to write a conditional to extract the nested generic type:
type ArrayType<A extends Array<any>> = A extends Array<infer T> ? T : never;
This seems unfortunate that we need to write a conditional since only the true branch will ever be satisfied. (Additionally, avoiding unnecessary usages of any would be nice.) We could avoid some duplication by dropping the constraint:
type ArrayType<A> = A extends Array<infer T> ? T : never;
But passing a non array into this type would move the error to wherever ArrayType was used (or perhaps even be silenced) instead of on the type parameter itself where the error actually occurred.
We could also do this with an extra type parameter, but this puts the onus on the user of the type to pass a correct value (and they can always just pass unknown or any):
type ArrayType<A extends Array<T>, T> = T;
type T2 = ArrayType<number[], number>;
type T3 = ArrayType<number[], unknown>;
type T1 = ArrayType<number[], any>;
Examples
type MyType<A, B> = { ... }
type OldLift<T extends MyType<any, any>, B> = T extends MyType<infer A, any>
? MyType<A, B>
: never;
type NewLift<T extends MyType<infer A, any>, B> = MyType<A, B>;
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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.
Research direction
The issue provides no file, test, or entry point; start by studying the proposed generic examples and the existing conditional-type behavior. The work is done when infer is accepted in generic type parameter extends clauses, preserves the stated constraint behavior, and has coverage for the ArrayType and OldLift/NewLift cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100