Avatar ·

How to convert a string to enum in TypeScript?

Hello everyone! I just started learning TypeScript and encountered a problem when working with enums. I have an enum describing order statuses: ```typescript enum OrderStatus { Pending = 'pending', Processing = 'processing', Shipped = 'shipped', Delivered = 'delivered', Cancelled = 'cancelled' } ``` I receive a string from the API (for example, from a JSON server response) that contains a status value, such as `'shipped'`. I need to convert this string back to the `OrderStatus` type so I can use it in a switch-case or pass it to a function expecting this enum. The problem is that a simple assignment doesn't work: ```typescript const statusFromApi: string = 'shipped'; const orderStatus: OrderStatus = statusFromApi; // Error: Type 'string' is not assign ```