在 Typescript 中有两种转换对象的方法。
<type>variable
和
variable as type
这两者似乎都是永久性(编译时)转换,因此转换后的对象将保留其类型。例如:
let number: number = 2;
let string1 = <string>number;
let string2 = number as string;
string1; // -> the type is string
string1; // -> the type is string
我的问题:两者有什么区别?
最佳答案
两者是一样的。引入了 foo as
字符串语法以避免与 JSX 语法(通常与 React 一起使用)产生歧义。
更多信息 here
关于typescript - <type>variable 和 variable as Typescript 中的类型有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41765688/