Type safety for enum property names #31112
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
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:
The text was updated successfully, but these errors were encountered: