You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to write a function to if a value is a member of an enum.
Use Cases
This is useful anytime you have external data you want to validate is a member of an enum, such as loading data from a network or a config file.
Examples
exportenumEnvironment{DEV='dev',STAGING='staging',SANDBOX='sandbox',PRODUCTION='production',}functionisValidEnvironment(env: string): env is Environment{// Argument of type 'string' is not assignable to parameter of type 'Environment'.ts(2345)returnObject.values(Environment).includes(env);}
This code used to work in older versions of TypeScript, but with the stricter definitions for Object.values the compiler now (correctly) says that the return value of Object.values is Environment[], and so I can't check if an enum includes a string without casting in some way:
returnObject.values<string>(Environment).includes(env);// or returnObject.values(Environment).includes(envasany);
Alternatively I tried making a generic enum membership function so I can limit my casting to a single place, but that didn't work very well. My TypeScript foo appears to be lacking.
functioninEnum<E=Object>(value: any,e: E): value is E{returnObject.values(e).includes(valueasany);}inEnum('foo',Environment)// `value is typeof Environment` which is wronginEnum<Environment>('foo',Environment)// Argument of type 'typeof Environment' is not assignable to parameter of type 'Environment'
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.)
Duplicate #10717 / #1394 - absent a more concrete suggestion, I'm assuming you want Array#includes or similar functions to allow supertypes of the element type (which is entirely reasonable).
Ah yes, I wasn't thinking about it from that perspective, making Array#includes to be able to take in more values. I was instead thinking of it from the point of view of being able to say that a enum was of a particular type in a generic. Happy to either leave this open or be closed as a duplicate of those tickets, whichever you think is more helpful.
Suggestion
I'd like to write a function to if a value is a member of an enum.
Use Cases
This is useful anytime you have external data you want to validate is a member of an enum, such as loading data from a network or a config file.
Examples
This code used to work in older versions of TypeScript, but with the stricter definitions for
Object.values
the compiler now (correctly) says that the return value ofObject.values
isEnvironment[]
, and so I can't check if an enum includes a string without casting in some way:Alternatively I tried making a generic enum membership function so I can limit my casting to a single place, but that didn't work very well. My TypeScript foo appears to be lacking.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: