-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Suggestion
For many type declarations, it shouldn't be necessary to use a conditional type which repeats a constraint already found in type parameter.
For example, instead of:
type ConstructorParameters<T extends abstract new (...args: any ) => any> =
T extends abstract new (...args: infer P) => any ? P : never;
This would be better expressed as:
type ConstructorParameters<T extends abstract new (...args: infer P) => any> = P;
🔍 Search Terms
infer type parameter conditional type type declaration
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
See above
📃 Motivating Example
TypeScript's own built-in types have other examples where constraints are repeated in both a type parameter and conditional type. I've also seen cases where developers just don't want to repeat constraints, so they define:
type RecordValueType<T> =
T extends Record<any, infer V> ? V : never;
And then incorrect usage results in a type of never
, rather than an error.
💻 Use Cases
- Improved readability
- Less repetition when authoring type declarations
- Less frustration when consuming lazily-declared types
treybrisbane, zardoy, also, kenbellows, Jak-Ch-ll and 15 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript