alp123
alp123

Reputation: 189

Typescript: add type for key in enum

I'm trying to iterate through an enum, but I'm struggling to get the typing right ..

This is my enum:

export enum WeekdayKeys {
    Monday = 'm',
    Tuesday = 'tu',
    Wednesday = 'w',
    Thursday = 'th',
    Friday = 'f',
    Saturday = 'sa',
    Sunday = 'su',
}

Here I'm iterating through the keys:

 Object.keys(WeekdayKeys).forEach((key: any) => console.log(WeekdayKeys[key]));

How can I correctly typeset the key value in the loop?

Upvotes: 0

Views: 138

Answers (1)

Slothyboi
Slothyboi

Reputation: 44

The answer TypeScript: Object.keys return string[] isn't exactly pretty for this, but it has been touched on before. As stated in the linked issue in the linked post, it's an intentional choice by the TS devs. See more about that issue on the issue post here.

Upvotes: 1

Related Questions