More efforts with typescript

Mar 18, 2022
export type Fruit = "Orange" | "Apple" | "Banana";
let myString: string = "Banana";

let myFruit: Fruit = myString as Fruit;

Also notice that when using string literals you need to use only one |

Edit

As mentioned in the other answer by @Simon_Weaver, it's now possible to assert it to const:

let fruit = "Banana" as const;

--

--

Jesse Chung
Jesse Chung

Written by Jesse Chung

0 Followers

Software Engineer of 3 years

No responses yet