microsoft / microsoft/TypeScript

Request: Infer generics

Aperta
#30,887 4 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo gli esempi di funzioni generiche nell'issue, concentrandoti su come infer gestisce gli argomenti e i valori restituiti delle funzioni. Leggi il contesto sull'inferenza generica di ordine superiore di TypeScript 3.4 e confrontalo con la discussione sulle funzioni variadiche nell'issue #30727. Il lavoro è completato quando i tipi generici vengono preservati attraverso infer senza perdere le informazioni previste sui parametri.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.