Typecript Enum access returns type 'string' rather than keyof typeof Enum, but expects keyof typeof Enum instead of 'string

Viewed 49

minimal example of problem:

enum EXAMPLE {
    FIRST,
    SECOND,
}

EXAMPLE[EXAMPLE[EXAMPLE.FIRST]]; // Error

Playground

according to this answer: https://stackoverflow.com/a/45520687 unless I'm misreading it, this should be correct?

Based on the compiled javascript, this type error makes no sense to me, and based on the what I presumed to be the internal type of the enum, this also makes no sense.

Why do enum types behave inconsistently regarding their keys? (i.e. why do they seem to be typed as something akin to Record<'FIRST'|'SECOND',0|1> & Record<0|1,string> rather than Record<'FIRST'|'SECOND',0|1> & Record<0|1,'FIRST'|'SECOND'>)

I can construct a pseudo-enum object that behaves the way I desire but I'd rather be able to use existing language features and gain the benefits thereof (conciseness, intellisense,etc.).

Is there a good fix for this, or something I'm missing?

0 Answers
Related