Typescript Type ‘string’ is not assignable to type

Question

Here’s what I have in fruit.ts
export type Fruit = “Orange” | “Apple” | “Banana”
Now I’m importing fruit.ts in another typescript file. Here’s what I have
myString:string = “Banana”;

myFruit:Fruit = myString;
When I do
myFruit = myString;
I get an error:
Type ‘string’ is not assignable to type ‘”Orange” | “Apple” | “Banana”‘
How can I assign a string to a variable of custom type Fruit?

0
ajeetsinghaassak@gmail.com 3 weeks 0 Answers 22 views 0

Leave an answer

Browse
Browse