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

Type safety for enum property names #31112

Open
5 tasks done
schoel-bis opened this issue Apr 25, 2019 · 1 comment
Open
5 tasks done

Type safety for enum property names #31112

schoel-bis opened this issue Apr 25, 2019 · 1 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

Comments

@schoel-bis
Copy link

Suggestion

Enums work in two directions, where an enum property value can be retrieved via AnEnum.aProperty and a property name can be retrieved via AnEnum[aValue]. In TypeScript 3.4.3, the type of the second expression is string. Instead, it should be equivalent to keyof typeof AnEnum | undefined.

Use Cases

While the nameof operator (suggested in #1579) does not exist yet, a construct such as AnEnum[AnEnum.aProperty] can currently be used to safely refer to an enum property name. Unfortunately, since the type of that expression is string it requires a cast when assigning it to a type safe target.

Examples

enum AnEnum { aProperty = 1, anotherProperty = 23, yetAnotherOne = 42 };
let target: keyof typeof AnEnum;



// Compilation error here: Type 'string' is not assignable to type '"aProperty" | "anotherProperty" | "yetAnotherOne"'.
target = AnEnum[AnEnum.aProperty];

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 Apr 25, 2019
@Howard-Lam-UnitedVanning
Copy link

Howard-Lam-UnitedVanning commented Jun 3, 2024

Would like this because for string enum, you can't really do the reverse keying like so Enum[Enum.key]. It needs to be nameof(Enum.key) / nameof Enum.key. Then, when there is refactoring, renaming will rename also the enum itself rather than just changing 1 string at a time.

Right now, my use case can be

export type RouteRecordCustom = RouteRecordRaw & {
    path: RoutePaths,
    name?: (keyof typeof RoutePaths)
};
routes: Array<RouteRecordCustom> =  = [
    {
        path: RoutePaths.Root,
        name: 'Root', //nameof RoutePaths.Root
        component: HomeView,
    },    {
        path: RoutePaths.HelloWorld,
        name: 'HelloWorld', //nameof RoutePaths.HelloWorld
        component: HelloWorld,
    },
]

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

3 participants