-
Notifications
You must be signed in to change notification settings - Fork 12.8k
iterating an enum is now an error #33123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I confirm, it happens to me as well, I couldn't find any way to overcome this problem. |
A workaround until the problem is solved is to use an interim object:
|
Still having this issue. |
As another alternative that feels a bit less like a work-around (at least to me) is to use type assertions after creating a new type based on the enum keys.
|
This was due to #31687 and was an intentional change:
Essentially, enum declarations became stricter since the domain of possible keys is a closed set, but the type provided by |
The workaround is as @bdunks describes: enum DAYS {
Monday = 'monday',
Tuesday = 'tuesday',
}
for (const a in DAYS) {
const x = DAYS[a as keyof typeof DAYS];
console.log(x);
} |
Iterating a string enum in strict mode is not allowed since 3.6.2. Is this a breaking change or a bug?
Sample code:
tsc --strict sample.ts
TSC 3.5.2: Compiles OK
TSC 3.6.2: reports an error:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof DAYS'.
The text was updated successfully, but these errors were encountered: