简体   繁体   中英

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM