microsoft / microsoft/TypeScript

enum to string (and number) literals keeping the opacity

Open
#38,658 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

enum typing, enum valueOf, enum string literal

Suggestion

The values of the strings enum are by design opaque it might however be useful to have a way to type them out of the enum.

This is what we have now:

const enum anEnum {
    a = 'A',
    b = 1,
}

type keysName = keyof typeof anEnum // "a" | "b"
type enumValues = typeof anEnum[keysName]; // anEnum.a | anEnum.b
type enumValueOf = ReturnType<enumValues['valueOf']>; // string | number

const aString: string = anEnum.a.valueOf(); // okay
const aNumber: number = anEnum.b.valueOf(); // okay

const notString: number = anEnum.a.valueOf(); // Type 'string' is not assignable to type 'number'.
const notNumber: string = anEnum.b.valueOf(); // Type 'number' is not assignable to type 'string'.

My feature request is to type the valueOf() using string literals and number "literals"

enum anEnum {
    a = 'A',
    b = 1,
}

type keysName = keyof typeof anEnum // "a" | "b"
type enumValues = typeof anEnum[keysName]; // anEnum.a | anEnum.b
type enumValueOf = ReturnType<enumValues['valueOf']>; // 'A' | 1

const aString: 'A' = anEnum.a.valueOf(); // okay
const aNumber: 0 = anEnum.b.valueOf(); // okay

const notString: number = anEnum.a.valueOf(); // Type 'A' is not assignable to type 'number'.
const notNumber: string = anEnum.b.valueOf(); // Type 0 is not assignable to type 'string'.

Use Cases

  • this is stricter and therefore never bad
  • this would allow having the values of an enum which would be useful to compare enums at compilation time (without messing with the opacity of the enum itself.)

Examples

const enum anEnum {
    a = 'A',
    b = 1,
}

const enum anotherEnum {
    a = 'A',
    b = 1,
}

// This is not allowed despite both enums are the same
const value: anotherEnum = anEnum.a;

// This would be allowed and safe (even with const enum)
type anotherEnumValues = ReturnType<typeof anotherEnum[keyof typeof anotherEnum]['valueOf']>;
const anOtherEnumValues: anotherEnumValues = anEnum.a;

Note: What I do not know is how this custom type could use a generic for the enum, as <T extends enum> is invalid. An enum is a bit weird in TS as it both represents a type and an 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files, tests, or implementation entry points are identified. Start by reviewing the enum typing and valueOf examples in the issue, then trace how enum member types are represented and tested in the TypeScript compiler. Done means string and numeric enum valueOf() results preserve their literal types without changing runtime output or enum opacity.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.