microsoft / microsoft/TypeScript
Suggestion: allow `infer T` in type parameter constraint of `type` statement
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
suggestion infer extends type parameter constraint
Suggestion
Allow infer T in the right of extends of a type statement.
Examples
This suggestion allows following code:
type ReturnType<T extends (...args: any) => infer R> = R;
Which behaves similarly as:
// this code is taken from lib.es5.d.ts
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
Use Cases
As demonstrated in the example, we sometimes use conditional types with the sole purpose of use of infer T.
We have to repeat some piece of code if we want to constraint what types can be passed to ReturnType.
So this suggestion provides an abbreviated syntax for this use case.
Open questions:
- Should union distribution occur for the new syntax?
My opinion is yes to align with existing conditional types- hmm, but then just replacing
anywithinfer _would change the behavior...
- Should function type parameters also allow
infer T?- No strong opinion
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
No source files, tests, or entry points are named. Start by reviewing the proposed infer T syntax and its conditional-type comparison, then resolve the open questions about union distribution and function type parameters. Done means the syntax and its semantics are specified well enough to implement and test.
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
- 30/100