Open schoel-bis opened 5 years ago
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,
},
]
Suggestion
Enums work in two directions, where an enum property value can be retrieved via
AnEnum.aProperty
and a property name can be retrieved viaAnEnum[aValue]
. In TypeScript 3.4.3, the type of the second expression isstring
. Instead, it should be equivalent tokeyof typeof AnEnum | undefined
.Use Cases
While the
nameof
operator (suggested in #1579) does not exist yet, a construct such asAnEnum[AnEnum.aProperty]
can currently be used to safely refer to an enum property name. Unfortunately, since the type of that expression isstring
it requires a cast when assigning it to a type safe target.Examples
Checklist
My suggestion meets these guidelines: