0

I have a situation where I need to dynamically use an object property. Typescript doesn't like when I do that..

The situation is similar as the one described above.

How can I type check the Enum[val] or foo variable?

enum Enum {
    VAR1 = "A",
    VAR2 = "B",
}

const foo = ["VAR1", "VAR2"];
foo.forEach(val => {
    const a = Enum[val]; // Typescript doesn't like it (val implicitly has 'any' type...)
    const b = Enum[val as any]; // This is "OK", but I use "any", which I'm trying to avoid.
});

1 Answer 1

3
const foo: (keyof typeof Enum)[] = ["VAR1", "VAR2"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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