3

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?

11
  • 1
    Object.values(Options) is an array whose element type is Options.Options1 | Options.Options2, also known as Options. If you want the type Options, you can just use it. Commented Dec 5, 2019 at 19:29
  • i don't want to type Options. i want 'xyz' | 'abc' Commented Dec 5, 2019 at 20:20
  • 1
    Are you aware that "xyz" | "abc" is a supertype of Options? Like this? TypeScript doesn't give you a way to automatically widen Options to "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 with Options? Commented Dec 5, 2019 at 20:27
  • 1
    Possible duplicate of getting a type for the values of a string enum Commented Dec 5, 2019 at 20:29
  • 1
    Note that suggestion here has been "use Options as 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? Commented Dec 5, 2019 at 20:34

1 Answer 1

1

You can use the Options enum as a type

enum Options {
        Option1 = "xyz",
        Option2 = "abc"
 }

let validValue: Options
validValue = Options.Option1

console.log(validValue) // xyz

// however, note that this is not allowed
// validValue = 'xyz'

This is another variation, not actually using enums

type Options2 = {
    Option1: 'xyz',
    Option2: 'abc'
}

type keys = keyof Options2 // 'Option1' or 'Option2'
type values = Options2[keys] // 'xyz' or 'abc'

let validValue2: values
validValue2 = 'xyz'
console.log(validValue2) // xyz (duh!)

// this is not allowed
// validValue2 = 'nope'
Sign up to request clarification or add additional context in comments.

3 Comments

i am restricted to use enum. the code doesn't compile in vscode. i get 'Options' refers to a value, but is being used as a type here. and i don't see how this will check for 'xyz' and 'abc'. also, you are manually using Options.Option1 here, i was looking for way to dynamically create the union type regardless of the name of the enum key...
@techguy2000 the code compiles fine on my machine and on typescriptlang.org/play. validValue can only be assigned one of the enum values (Options.Option1 or Options.Option2), so the compiler essentially checks for it to be either xyz or abc, no other values are possible.
my bad. i was const Options = MyOriginalEnum for the original post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.