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

Allowing you to check membership of an enum #35215

Closed
5 tasks done
aboyton opened this issue Nov 20, 2019 · 3 comments
Closed
5 tasks done

Allowing you to check membership of an enum #35215

aboyton opened this issue Nov 20, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@aboyton
Copy link

aboyton commented Nov 20, 2019

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

export enum Environment {
  DEV = 'dev',
  STAGING = 'staging',
  SANDBOX = 'sandbox',
  PRODUCTION = 'production',
}

function isValidEnvironment(env: string): env is Environment {
  // Argument of type 'string' is not assignable to parameter of type 'Environment'.ts(2345)
  return Object.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:

return Object.values<string>(Environment).includes(env);
// or 
return Object.values(Environment).includes(env as any);

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.

function inEnum<E = Object>(value: any, e: E): value is E {
  return Object.values(e).includes(value as any);
}

inEnum('foo', Environment) // `value is typeof Environment` which is wrong
inEnum<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.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Nov 20, 2019

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).

@aboyton
Copy link
Author

aboyton commented Nov 20, 2019

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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 22, 2019
@RyanCavanaugh
Copy link
Member

Yep, any alternate to those would be out of scope anyway. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants