microsoft / microsoft/TypeScript

[Feature] Utility type for T[keyof T] or new keyword valueof

Offen
#37,642 3 Kommentare 34 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Search Terms

typeof keyof utlity type

Suggestion

Alternative 1:
Create a new Utility type expressing T[keyof T]
Example Value<T> = T[keyof T].

Alternative 2:
Create a new keyword equivalent to T[keyof T]
Example valueof T = T[keyof T]

Use Cases

While T[keyof T] is short and concise, from my experience a lot of developers new to TypeScript usually have no idea what this means or why it's written like that when they first see it.
I also feel that it's not as easy to Google a the answer to that, in comparison to established utility and advanced types like Pick, Omit, Record, etc. which all have their own entries in the TS documentation.

Additionally Object.keys and Object.values are similar concepts.
The presence of a keyof keyword would suggest a valueof should also exist, yet it's missing and one must employ a "special" approach to replicate.

A simple solution is to write my own custom type for the project, but that just introduces additional abstraction and the knowledge/practice isn't transferable to other projects, without implementing the same custom type somewhere in the code first.

And lastly, and this is not a big issue, but I feel it's redundant having to write the type for T twice in on the same line just a few symbols apart.
With short names such as T it's okay, but with longer type names the code becomes more cumbersome to read.

Examples

type T = {
    a: string;
    b: number
    c: number[]
    ...etc
}

// Value<T> = string | number | number[]...
// valueof T = string | number | number[]...

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.
    Perhaps some conflict on point 8...

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es werden keine Dateien, Tests oder Einstiegspunkte genannt. Beginne damit, den vorgeschlagenen Utility-Typ Value<T> und die Alternativen mit dem Schlüsselwort valueof T zu überprüfen, und ermittle anschließend den erforderlichen Umfang bei Sprachdesign und Compiler, um zwischen ihnen zu wählen. Als erledigt gilt die Aufgabe, wenn ein festgelegtes Design und ein Implementierungsplan vorliegen, aber das Issue definiert keine Abnahmetests.

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
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.