microsoft / microsoft/TypeScript
Request: Infer generics
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
The request is to let infer support generics.
Background:
I recently published a recursive Pipe and Compose. It offers some key advantages, including the preservation of variable names.
https://github.com/babakness/pipe-and-compose-types
Its shortcoming is in the case of generics. This is because infer does not work well with generics. Example
/**
* Extracts function arguments
*/
export type ExtractFunctionArguments < Fn > = Fn extends ( ...args: infer P ) => any ? P : never
/**
* Extracts function return values
*/
export type ExtractFunctionReturnValue<Fn> = Fn extends ( ...args: any[] ) => infer P ? P : never
type Foo = ExtractFunctionArguments< <A>(a:A, b:number) => A >
// Foo has type [ {} , number ]
TypeScript 3.4 improved how pipe and compose functions work with generics with higher order type inference from generic functions.
Common implementations of pipe and compose rely on parameter overloading. However, even here, there is an issue with variadic functions. See example in this issue:
#30727
The broader issue with the parameter overloading version of pipe and compose is that parameter names are lost, decreasing code clarity. The recursive version, relying on infer does maintain parameter names.
Providing support generics with infer will present a major productivity bump and will nearly complete the community's need for the common place pipe and compose in FP patterns without compromise.
The only outlier is a small situation with parameter names on variadic functions of a minimum arity of zero as opposed to at least one. Can provide examples if needed since it is a separate issue.
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 damit, die Beispiele für generische Funktionen im Issue zu reproduzieren, und konzentriere dich darauf, wie infer Funktionsargumente und Rückgabewerte verarbeitet. Lies den Kontext zur Inferenz höherer generischer Funktionen aus TypeScript 3.4 und vergleiche ihn mit der Diskussion zu Funktionen mit variabler Argumentanzahl in Issue #30727. Als erledigt gilt die Aufgabe, wenn generische Typen durch infer erhalten bleiben, ohne die beabsichtigten Parameterinformationen zu verlieren.
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