I have this enum:
enum Options {
Option1 = "xyz",
Option2 = "abc"
}
I want to use the values for type checking by creating a union type of 'xyz' | 'abc'. Here is my attempt, but I get this 'const' assertion error:
const validValues = Object.values(Options);
const validKeys = validValues as const;
~~~~~~~~~~~ A 'const' assertion can only be applied to references to
enum members, or string, number, boolean, array, or object
literals.
What is the proper way to do this?
Object.values(Options)is an array whose element type isOptions.Options1 | Options.Options2, also known asOptions. If you want the typeOptions, you can just use it."xyz" | "abc"is a supertype ofOptions? Like this? TypeScript doesn't give you a way to automatically widenOptionsto"xyz" | "abc", but it's not clear why you need this. Maybe you could edit in a use case for your code? What specifically do you need to do with the type"xyz" | "abc"that you currently cannot do withOptions?Optionsas your type". If I do that (using[K in Options]instead of[K in keyof typeof Options]) it looks good to me. What specifically is the problem?