question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: support `static enum`

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
klippas-robcommented, Jan 10, 2022

This is definitely NOT a question. This functionality is needed - I only want to define an enum once and import it for use in multiple classes where static objects are defined.

1reaction
RyanCavanaughcommented, Jan 11, 2022

This suggestion has not garnered the quantity or quality of feedback that would lead us to prioritize it over the (again, thousands of) other open feature requests in this repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Enum Support for Query Parameters - Redash Discourse
Add Enum Support for Query Parameters · Feature Requests ... While it's cucumbersome, you can implement the static list as a “static” query:...
Read more >
java - Why can't enums be declared locally in a method?
Enums are static nested classes because they define static member variables (the enum values), and this is disallowed for inner classes: ...
Read more >
Feature Request: sealed-enum (and maybe non-sealed enum)
This new feature allows a parent class to specify which child classes can extend from the parent. You simply add the child class...
Read more >
A class-based enum pattern for JavaScript - 2ality
In this blog post, we examine a pattern for implementing enums in ... One occasionally requested feature for enums is that enum values...
Read more >
Using Enumeration classes instead of enum types
Instead, you can create Enumeration classes that enable all the rich features of an object-oriented language. However, this isn't a critical ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found