How to convert a string to enum in TypeScript?
Hello everyone! I'm new to TypeScript and encountered a problem when working with enums. I have an enum for order statuses:\n\n```typescript\nenum OrderStatus {\n Pending = "pending",\n Processing = "processing",\n Shipped = "shipped",\n Delivered = "delivered",\n Cancelled = "cancelled"\n}\n```\n\nI receive a status from the API as a string, for example `"processing"`. I need to convert this string back to an enum value to use it in switch/case or compare with other values. Simple type casting via `as OrderStatus` works unreliably — if the string doesn't match any of the values, the code doesn't produce a compilation error, but unpredictable behavior occurs at runtime.\n\nI also tried using `OrderStatus["processing"]`, but it returns
No answers yet.
Log in to leave an answer