microsoft / microsoft/TypeScript
Allow inferring rest element types in conditional types involving tuples
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
rest element infer tuple
Suggestion
Currently, inferring single elements of tuple types is possible using the infer keyword:
type FirstArg<T extends any[]> =
T extends [infer R, ...any[]] ? R :
T extends [] ? undefined :
never;
type T1 = FirstArg<[number, 2, 3]>; // number
type T2 = FirstArg<[1, 2, 3]>; // 1
type T3 = FirstArg<["", 2]>; // ""
type T4 = FirstArg<[]>; // undefined
However it is not possible to infer the type of the remaining arguments in one go, except by resorting to functions:
type RestArgs<T extends any[]> =
T extends [any, infer R[]] ? R : // this does not work - no way to specify that R should be an array!
T extends [any] ? []] :
never;
// this does
type RestArgs<T extends any[]> =
((...args: T) => void) extends ((first: any, ...rest: infer S1) => void) ? S1
: T extends [infer S2] ? []
: T extends [] ? []
: never;
type T1 = RestArgs<[1,2,3]>; // [2, 3]
type T2 = RestArgs<[1,2]>; // [2]
type T3 = RestArgs<[1]>; // []
type T4 = RestArgs<[]>; // []
I would like to see the possibility to infer rest types in tuples, e.g. like this (square brackets):
type RestArgs<T extends any[]> = T extends [any, infer R[]] ? R : never;
or like this (3 dots)
type RestArgs<T extends any[]> = T extends [any, infer ...R] ? R : never;
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. new expression-level syntax)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La issue fornisce esempi di tipi condizionali e tuple, ma non indica file di implementazione o test. Inizia individuando la logica di inferenza dei tipi condizionali del compilatore e i test esistenti per l'inferenza delle tuple, quindi verifica che l'inferenza rest proposta produca i risultati delle tuple mostrati senza modificare il comportamento esistente.
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
- 35/100