microsoft / microsoft/TypeScript

Allow use of infer in extends clause of generic type parameter

Aperta
#29,378 0 commenti 9 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

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.

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

La issue non fornisce alcun file, test o punto di ingresso; inizia studiando gli esempi generici proposti e il comportamento esistente dei tipi condizionali. Il lavoro è completato quando infer viene accettato nelle clausole extends dei parametri di tipo generici, viene preservato il comportamento dei vincoli indicato e sono presenti test per i casi ArrayType e OldLift/NewLift.

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.