microsoft / microsoft/TypeScript

Flexibility of template literal types for enums

Offen
#41,649 2 Kommentare 3 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
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

template literal type enum permutations

Suggestion

Typescript 4.1 brought template literal types, which make it simple to express all the possibilities of permutations of unions of string types concatenated together. It would be cool if there was some equivalent syntax for expressing enum key/value pairs with the same level of expressivity that template literal types provide.

Use Cases

However, my codebase right now uses enums for these kinds of string types, so if I want to express a bunch of permutations of possible values with an enum, I can't really leverage template literal types in this case.

Examples

this is just some syntax I'm making up, I don't know how this would work especially for dealing with keys and values separately:

enum Color {
  RED,
  BLUE
}
enum Shape {
  SQUARE,
  CIRCLE
}
enum ColoredShape {
  [`${keyof Color}_${keyof Shape}`]
}

console.log(ColoredShape.RED_SQUARE, ColoredShape.BLUE_CIRCLE)

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.
    • I think?

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

Beginne mit der Prüfung der im Issue vorgeschlagenen Interaktion zwischen template literal types und enum-Schlüssel-/Wertepaaren. Der Payload nennt keine Dateien, Tests oder Einstiegspunkte, und Syntax und Semantik bleiben spekulativ. Erledigt wäre die Aufgabe erst, wenn ein festgelegtes Design für enum-Permutationen sowie ein klarer Implementierungs- und Validierungsplan vorliegen.

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
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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