1

I would like to use enum keys as keys in another object, but I want them to be lowercase. I tried bunch of ways to solve it, however I can't find a solution to this problem. Is there a way to tell Typescript that the enum keys are only strings and they will never be numbers?

Consider this:

enum Error {
  SUCCESS = "Success",
  ERROR = "Error",
}

type ErrorKeys = Lowercase<keyof Error> // type number is not assignable to string
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Terminat
  • 1,199
  • 5
  • 21
  • The type `Error` here refers to `"Success" | "Error"`, so `keyof Error` is `number | "toString" | "charAt" | "charCodeAt" ...`. You probably want `keyof typeof Error`, which is `"SUCCESS" | "ERROR"`. – Lauren Yim Mar 02 '21 at 11:19
  • Yes, that's exactly what I needed @cherryblossom – Terminat Mar 02 '21 at 11:25

0 Answers0