且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Typescript类型'string'不能分配给类型

更新时间:2022-11-30 12:07:17

你需要强制转换

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 |