microsoft / microsoft/TypeScript

Feature request: support `static enum`

Offen
#34,674 13 Kommentare 1 Reaktion 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

enum reflection, const enum, const enum literal, const enum reference, const enum export

Suggestion

Add a new type of enum, say static enum, that works similar to const enum, but instead of inlining const enum, exporting enum values as constants, and emit code that refers to it.

Use Cases

  1. Long string enum values

    In case long strings are present in a const enum, using such enum in various places will emit code that is way too large. It would be nice to emit code that uses references to the constants instead of literals.

  2. Non-literal enum values

    More than often, we want to export a set of constants of certain types that can be passed to a function as an enum. For instance, in a base64 module, an enum representing the charset to use for encoding and decoding. In an icon module for react, an enum representing possible svg icons to display.

Some existing alternatives:

  • enum

    enum emits clumsy code with runtime reflection support. However, when being used statically reflection is not needed, which makes performing dead code elimination really hard (unless closure compiler is used, which is almost infeasible when npm packages are used.)

  • export const ENUM_VALUE;

    Practically, exporting a const works. However, there is no way to declare a type for those ENUM_* constants. This makes it difficult to design an API that can enforce the typing on the enum (or at least let code editor provide sensible autocompletion when a non-literal type is used.)

Examples

icons.ts

export static enum Icons {
  SMILY_FACE = "some long svg path";
  SAD_FACE = "some long svg path";
  OBJECT = { name: "object literal", path: "some long svg path" }; // as we are just exporting consts, non literals are also ok.
}

app.tsx

import { Icons } from './icons';

<Icon icon={Icons.SMILY_FACE} />
<Icon icon={Icons.OBJECT} />

Transpilied into:

icons.js

export const Icons_SMILY_FACE = "some long svg path";
export const Icons_SAD_FACE = "some long svg path";
export const Icons_OBJECT = { name: "object literal", path: "some long svg path" };

// N.B. this is just named exports, so any current bundler will take care of unused ones.

app.js

import { Icons_SMILY_FACE, Icons_OBJECT } from './icons';

react.createElement(Icon, { icon: Icons_SMILY_FACE });
react.createElement(Icon, { icon: Icons_OBJECT });

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.

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 Implementierungsdateien oder Tests genannt. Beginne damit, den static-enum proposal und seine TypeScript- und JavaScript-Beispiele zu prüfen, und ermittle dann die Compiler- und Test-Einstiegspunkte, die benötigt werden, um Syntax, Typprüfung und ausgegebene Exporte zu definieren; abgeschlossen ist die Aufgabe, wenn ein abgestimmtes Design mit dazu passender Implementierung und passenden Tests vorliegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, 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

Neue Issues direkt in Ihr Postfach

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