-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Enumerating an enum: Wrong thing is allowed and right thing is not with with noImplicityAny #39627
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 don't know about
This is just not true - try printing out enum Test2 {
One = "one",
Two = 2
}
for (const key in Test2) {
console.log(typeof key);
} Further, |
I wasn't taking about the actual runtime typeof key. That will indeed match what it actually is. I was referring to what the VS IDE shows if you hover over key in that for loop. Trying the following test:
Shows that although the IDE shows it as string, the compiler is aware that the loop variable isn't just any string. I think ultimately if there's one thing that should be fixed it's really the third thing: Fail the compile for for...in on an enum. It doesn't matter how that's flagged. Other observations / suggestions are there in case they help identify a deeper issue in type inference. |
This is the intended behavior, or at least the designed behavior. The iterating variable in a const arr: boolean[] = [true, false, true];
for (const i in arr) {
console.log(arr[i]);
} work as expected. This interacts a bit weirdly with the reverse lookup on enums, but broadly speaking it's what we want to happen. "Mixed" enums like |
Consider this code:
enum Test1 {
One = 1,
Two = 2
}
enum Test2 {
One = "one",
Two = 2
}
enum Test3 {
One = "one",
Two = "two"
}
for (const key in Test1) {
const aValue = Test1[key];
console.log(aValue);
}
This code compiles but because in an enum a reverse value->key map is also set up for numeric values, the outcome is not what one would expect: 1, 2. It's: One, Two, 1, 2 instead. Similarly, replacing Test1 with Test2 in the for..in loop compiles but gives unexpected result: "Two", "one", 2. However, with Test3, for which the result would've been correct: "one", "two", there's a compile error with the noImplicitAny compiler option:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Test3'.
No index signature with a parameter of type 'string' was found on type 'typeof Test3'.
This seems backwards of what the behavior should be: With Test1 and Test2, the compile should fail and it should succeed with Test3. If not that, the compile should either fail or succeed with all 3. FYI, the reason I noticed this was because converting an all numeric enum to all string enum caused compile failure for some other code that was using "for..in" which originally had a logical flaw which the compiler didn't catch but it's flagging it now when the logical flaw has (inadvertently) been fixed.
It seems like there are at least a couple of independent problems here.
With enums that have at least one numeric member (Test1 and Test2), the inferred type of loop variable (key) should ideally be the union of all keys and numeric values or at least string | number, not just string.
Indexing enums by a string (that cannot be narrowed down specific literals representing keys of an enum) should consistently return any. It may acceptable to return union type of all possible values: number for Test1, string|number for Test2 and string for Test3 and not string for Test1 and Test2 and any for Test3.
In addition to the issues above, a for..in over an enum should somehow be flagged and fail to compile regardless of noImplicitAny flag because it only yields the expected result in the enum has all string values but even with all string-enums, that would be fragile as adding a single numeric value to the enum will cause compile failures at that point. It should just not be allowed at least by default.
Issue #18409 seems related but not quite the same.
The text was updated successfully, but these errors were encountered: