FG
๐ŸŒ Web & Full-StackMicrosoft

Allow enums of types other than number

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

I'm reopening this issue, because it was closed with the move from codeplex, and doesn't seem to have been re-opened. https://typescript.codeplex.com/workitem/1217 I feel like this is very important for a scripting language.-- Especially for objects that are passed back and forth over web service calls.-- People generally don't use integer based enums in json objects that are sent over the wire, which limits the usefulness of the current enum implementation quite a bit. I would like to re-propose the original contributor's suggestions verbatim.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Enhance TypeScript Enum Implementation to Support String and Other Types

Medium Risk

The current TypeScript enum implementation is limited to numeric values, which restricts its usability in scenarios where JSON objects are exchanged over web services. This limitation prevents developers from using more expressive string-based enums, which are commonly used in APIs and can improve code readability and maintainability.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Modify Enum Declaration Syntax

    Update the TypeScript compiler to allow enums to be declared with string values, in addition to numeric values. This involves modifying the parser to recognize string literals as valid enum members.

    typescript
    enum Color { Red = 'RED', Green = 'GREEN', Blue = 'BLUE' }
  2. 2

    Update Enum Value Resolution Logic

    Adjust the logic in the TypeScript compiler that resolves enum values to ensure that both string and numeric values are handled correctly during compilation and runtime. This includes updating the symbol table and type checking mechanisms.

    typescript
    // Example of enum usage
    const myColor: Color = Color.Red;
  3. 3

    Revise Documentation and Examples

    Update the TypeScript documentation to include examples of string-based enums and their usage. Ensure that the new features are clearly explained to help developers transition to the updated enum implementation.

    typescript
    // Documentation example
    enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' }
  4. 4

    Implement Backward Compatibility Checks

    Ensure that the changes do not break existing code that relies on numeric enums. Implement tests to verify that both old and new enum declarations work seamlessly in the same codebase.

    typescript
    // Test for backward compatibility
    const oldEnum: OldEnum = OldEnum.Value1; // Should still work
  5. 5

    Conduct Comprehensive Testing

    Run a suite of tests covering various use cases for enums, including serialization and deserialization to/from JSON. Ensure that both string and numeric enums behave as expected in all scenarios.

    typescript
    // Test serialization
    const json = JSON.stringify({ color: Color.Green });

Validation

To confirm the fix worked, create a TypeScript project that uses both string and numeric enums. Compile the project and ensure there are no errors. Additionally, serialize and deserialize objects containing these enums to/from JSON and verify the integrity of the data.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typescriptcompilersuggestionfixedcommitted