microsoft / microsoft/TypeScript
Allow use of `infer` in the type parameter of a type declaration
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den motivierenden Beispielen und der vorgeschlagenen infer-Syntax im Issue und prüfe anschließend, wie TypeScript derzeit Einschränkungen von Typparametern und bedingte Typen behandelt. Die Arbeit ist abgeschlossen, wenn Typdeklarationen einen Typparameter direkt aus seiner Einschränkung ableiten können, ohne einen bedingten Typ zu wiederholen, und dabei das angegebene Verhalten hinsichtlich Lesbarkeit und Validierung erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100