microsoft / microsoft/TypeScript
Allow use of infer in extends clause of generic type parameter
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
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.
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
Das Issue enthält keine Datei, keinen Test und keinen Einstiegspunkt; beginne damit, die vorgeschlagenen generischen Beispiele und das bestehende Verhalten bedingter Typen zu untersuchen. Die Arbeit ist abgeschlossen, wenn infer in extends-Klauseln generischer Typparameter akzeptiert wird, das angegebene Constraint-Verhalten erhalten bleibt und Testabdeckung für die Fälle ArrayType und OldLift/NewLift vorhanden ist.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100