javascript - Typescript Type 'string' is not assignable to type

Published: 02 July 2023
on channel: Code Samples
193
2

#short
#javascript
#typescript
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?
cast it: http://www.typescriptlang.org/docs/ha...
string literals: https://www.typescriptlang.org/docs/h...


Watch video javascript - Typescript Type 'string' is not assignable to type online without registration, duration hours minute second in high quality. This video was added by user Code Samples 02 July 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 193 once and liked it 2 people.