Qwertiy
Qwertiy

Reputation: 21430

Use const enum keys (not values) as interface properties

In following code variable x has properties a and z, but I want it to have a and b. How can I express that in typescript?

const enum CE {
    a = "a",
    b = "z",
}

declare var x: Record<CE, any> // has 'a' and 'z'

Upvotes: 1

Views: 190

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 37988

Enum is variable itself, so you can query its type with typeof CE, then get its keys with keyof:

declare var x: Record<keyof typeof CE, any> // Record<"a" | "b", any>

Playground

Upvotes: 1

Related Questions