microsoft / microsoft/TypeScript

Preserve tuples when mapping with `as` clauses

Aperta
#40,586 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Search Terms

#tuple #mapped #as #clause #preserve

Suggestion

New mapped type's as clause allows one to filter the entries of an object. Basic mapped types preserve the shape of a tuple but not when used with the new as clause:

type tuple = [0, 1, 2]

type Mapped<T> = {
    [K in keyof T]: T[K] | undefined
}

type MappedAs<T> = {
    [K in keyof T as K]: T[K] | undefined
}
type test0 = Mapped<tuple>   // tuple shape is preserved
type test1 = MappedAs<tuple> // tuple shape is object now
test0 === [0 | undefined, 1 | undefined, 2 | undefined]
test1 === {
    [x: number]: 0 | 1 | 2 | undefined;
    0: 0 | undefined;
    1: 1 | undefined;
    2: 2 | undefined;
    length: 3 | undefined;
    toString: (() => string) | undefined;
    toLocaleString: (() => string) | undefined;
    ...
}

Use Cases

  • Filtering lists without having to rely on costly recursive types

Examples

type FilterNumbers<T> = {
    [K in keyof T as T[K] extends number ? never : K]: T[K] | undefined
}

type test2 = FilterNumbers<[1, '2', 3, '4']> // ['2', '4']

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

Inizia riproducendo in TypeScript gli esempi di tuple dell’issue e confronta i risultati dei mapped types con e senza una clausola as. Analizza la gestione dei mapped types da parte del compilatore, quindi aggiungi una copertura che dimostri che il filtraggio di una tupla preserva la forma della tupla e verifica che l’esempio produca la tupla filtrata attesa.

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.