microsoft / microsoft/TypeScript

Suggestion: allow `infer T` in type parameter constraint of `type` statement

Aperta
#39,736 2 commenti 3 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

Search Terms

suggestion infer extends type parameter constraint

Suggestion

Allow infer T in the right of extends of a type statement.

Examples

This suggestion allows following code:

type ReturnType<T extends (...args: any) => infer R> = R;

Which behaves similarly as:

// this code is taken from lib.es5.d.ts
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

Use Cases

As demonstrated in the example, we sometimes use conditional types with the sole purpose of use of infer T.
We have to repeat some piece of code if we want to constraint what types can be passed to ReturnType.
So this suggestion provides an abbreviated syntax for this use case.

Open questions:

  • Should union distribution occur for the new syntax?
    • My opinion is yes to align with existing conditional types
    • hmm, but then just replacing any with infer _ would change the behavior...
  • Should function type parameters also allow infer T?
    • No strong opinion

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.

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

Non vengono indicati file sorgente, test o punti di ingresso. Inizia esaminando la sintassi infer T proposta e il relativo confronto tra tipi condizionali, quindi risolvi le questioni aperte sulla distribuzione delle unioni e sui parametri di tipo delle funzioni. Il lavoro è completato quando la sintassi e la relativa semantica sono specificate abbastanza dettagliatamente da poter essere implementate e testate.

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
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.