-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
String literals not evaluate to string enum values. #22855
Comments
To expand on this, the same behavior happens with matching string literal types.
|
@electricessence: As a workaround you could use a custom type-guard function with function overloading:
|
@MartinJohns yeah I know that I can use custom type guards but that's what I'm trying to get away from when it's unnecessary. I have had success with building a virtual enum by declaring exported constants each as the string literal types. But I was hoping for the same behavior from enums. |
What I like about the string enum types is that they function as both a type and as set of constants. |
String enums are intentionally opaque; see #17690. The |
@RyanCavanaugh ok. I actually was using that pattern before. Thought it was more elegant to use enums. |
TypeScript Version: 2.8.0
Search Terms:
string literal enum
Code
Expected behavior:
The code in the within the if block should understand that x is a string.
Actual behavior:
The if block is unaware of the type.
The text was updated successfully, but these errors were encountered: