microsoft / microsoft/TypeScript

Mapped Type `[Lhs of Rhs]` Syntax for Array and Tuple Types

Aperta
#54,575 2 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Suggestion

🔍 Search Terms

mapped type, of, array, tuple

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

A new [Lhs of Rhs] syntax in mapped type to map over values of array and tuple types:

type ResultType = {
    [V of SourceType]: DoSomethingWith<V>
}

Rhs does not need to be a type parameter.
Rhs must extend readonly unknown[] or a type error is raised.

📃 Motivating Example

type Foo = ['hello', 'world']

type Bar = {
    [V of Foo]: V[]
}
// Bar = ['hello'[], 'world'[]]

💻 Use Cases

Currently, to do the same as in motivating example, one might intuitively write:

type Bar = {
    [K in keyof Foo]: Foo[K][]
}

However this is incorrect and is a mistake that's potentially difficult to spot.

Instead one must either introduce a generic intermediate type:

type Transform<T> = {
    [K in keyof T]: T[K][]
}

type Bar = Transform<Foo>

Or use the infer workaround:

type Bar = Foo extends infer T ? {
    [K in keyof T]: T[K][]
} : never

Both workarounds are not immediately obvious to inexperienced developers, less ergonomic, and will silently break when Foo is no longer an array or tuple type without putting extra constraints.

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 confrontando gli esempi proposti di [Lhs of Rhs] con il workaround esistente basato su keyof per i mapped types e il suo comportamento per array e tuple. Individua quindi le aree del compilatore e i test che regolano i mapped types, poi definisci la sintassi accettata, il vincolo readonly unknown[], i tipi tupla risultanti e le diagnostiche per i membri destri non validi.

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.