You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enums as union types aren't compatible with strings (5.0) #53304
The issue is that this seems to only work with numbers, but not with other types like strings. If we take the exact same code but replace all numbers with strings, we have this:
enumE{A='42',}exportletfine: E='42';// fails but should work// ^exportletoops: E='43';// fails as expected// ====enumF{Foo='10',Bar='20',}functiontakeValue(e: F){}takeValue(F.Foo);// workstakeValue('10');// fails but should work// ^takeValue('123');// fails as expected
🙁 Actual behavior
The error Type '"42"' is not assignable to type 'E'. (2322) is sent by TS for the code let fine: E = '42';.
🙂 Expected behavior
This shouldn’t behave differently from numbers, so writing let fine: E = '42'; could be allowed by TS
changed the title [-]Enums as union types aren't compatible with strings[/-][+]Enums as union types aren't compatible with strings (5.0)[/+]on Mar 17, 2023
If that's expected, should the release note be updated to highlight the fact that this only works for numbers and not for all types?
It mentions that enums started as "a set of numeric constants with the same type" and that in the example "E.Foo and E.Bar (...) were pretty much just numbers".
But it doesn’t explicitly say that the union feature can only work with numbers (specially when it mentions "Enum literal types gave each enum member its own type", and later "That means that all enums can now be narrowed and have their members referenced as types as well", which can lead to a bit of confusion).
I think you’re right notaphplover, this comment was a reply of a question in the PR exactly about the behavior mentioned by this issue. So it doesn’t seem to be a bug.
Activity
[-]Enums as union types aren't compatible with strings[/-][+]Enums as union types aren't compatible with strings (5.0)[/+]fatcerberus commentedon Mar 17, 2023
AFAIK string enums are intentionally stricter than numeric ones, so this is probably by design.
notaphplover commentedon Mar 17, 2023
It's doesn't seem to be a bug. Consider this message from the lead architect of TypeScript.
Ayc0 commentedon Mar 17, 2023
Thanks for the information! 🙏
If that's expected, should the release note be updated to highlight the fact that this only works for numbers and not for all types?
It mentions that enums started as "a set of numeric constants with the same type" and that in the example "E.Foo and E.Bar (...) were pretty much just numbers".
But it doesn’t explicitly say that the union feature can only work with numbers (specially when it mentions "Enum literal types gave each enum member its own type", and later "That means that all enums can now be narrowed and have their members referenced as types as well", which can lead to a bit of confusion).
Ayc0 commentedon Mar 17, 2023
I think you’re right notaphplover, this comment was a reply of a question in the PR exactly about the behavior mentioned by this issue. So it doesn’t seem to be a bug.
I’ll close this as completed.
devlopersabbir commentedon Jul 29, 2024
Here is a simple way to do that.
Here the example of how to convert a Typescript enum to a union type
And it’s pretty easy
Reference Link