-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Search Terms
enum isEnum mapped-types conditional-types enum-test extends-enum
Suggestion
Rephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-types
Here's what I kinda want, but it is not syntactically valid:
enum Features {
"A" = 1,
"B" = 2,
"C" = 2
}
type EnumOrString<T> = T extends enum
? T | keyof T
: T
declare function getData(featureFilter: EnumOrString<typeof Features>[]): Features[]getData takes an array of the enum values or the enum keys but returns only the enum values.
I also would want to extend this to mapped types similar to DeepPartial so that any nested enums all get this treatment - without having to have separate hierarchies of types partitioned by Request and Response. Example on Playground
Use Cases
I make calls to restful services that accept enum values as either the number value OR the string key, but the services always return just the number value. I could ignore the ability to send strings, but some code would have to be rewritten because it happens to cache values by these key names.
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. new expression-level syntax)