microsoft / microsoft/TypeScript
Mapped Type `[Lhs of Rhs]` Syntax for Array and Tuple Types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die vorgeschlagenen [Lhs of Rhs]-Beispiele mit dem bestehenden keyof-Workaround für mapped types und seinem Verhalten für Arrays und Tupel zu vergleichen. Ermittle anschließend die Compilerbereiche und Tests, die mapped types steuern, und definiere dann die akzeptierte Syntax, die readonly unknown[]-Constraint, die resultierenden Tupeltypen und die Diagnosen für ungültige rechte Seiten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100