'Checking Typescript Enum for value results in TS error OR doesn't work
The following code does what I want it to do:
BallStatusTypes.safe === BallStatusTypes[status] ? 'ball' : 'ball-danger'
Despite giving me this error: This condition will always return 'false' since the types 'BallStatusTypes' and 'string' have no overlap.
The following, error free code does NOT do what I want it to do:
status === BallStatusTypes.safe ? 'ball' : 'ball-danger'
The enum is:
export enum BallStatusTypes {
danger = 1,
safe = 2
}
How can I fix my code so that it does what I want it to do, without errors?
Solution 1:[1]
I ended up resolving this with:
export enum BallStatusTypes {
danger = 'danger',
safe = 'safe'
}
It seems I was having issues with the way enums create objects mapping to either a number or a string. And, typescript was unaware (when performing expressions on said value) of weather or not it should be comparing the numerical version or the string version of the status.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nuzzlet |
