Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum to string (and number) literals keeping the opacity #38658

Open
5 tasks done
Xample opened this issue May 19, 2020 · 0 comments
Open
5 tasks done

enum to string (and number) literals keeping the opacity #38658

Xample opened this issue May 19, 2020 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Xample
Copy link

Xample commented May 19, 2020

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.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants