为什么打字稿中有两种不同的转换语法?
Why there are 2 different syntax of casting in typescript?
据我所知,在 Typescript 中有两种类型的语法。
只需使用<
和>
来施放
const a = <A>b;
使用as
语句来投
const a = b as A;
我猜1或2生成的代码是完全一样的。
而且,当我开始使用 Typescript 时,还没有这样的语法来使用 as
语句。我猜 as
是打字稿中的新语法。
但是,应该有一些理由来制定新的语法。为什么他们需要这些语法?如果只有一种强制转换语法会有什么不方便吗?
如描述here
Originally the syntax that was added was <foo>
.
...
However there is an
ambiguity in the language grammar when using <foo>
style assertions in
JSX:
var foo = <string>bar;
</string>
Therefore it is now recommended that you just use as foo
for
consistency.
据我所知,在 Typescript 中有两种类型的语法。
只需使用
<
和>
来施放const a = <A>b;
使用
as
语句来投const a = b as A;
我猜1或2生成的代码是完全一样的。
而且,当我开始使用 Typescript 时,还没有这样的语法来使用 as
语句。我猜 as
是打字稿中的新语法。
但是,应该有一些理由来制定新的语法。为什么他们需要这些语法?如果只有一种强制转换语法会有什么不方便吗?
如描述here
Originally the syntax that was added was
<foo>
.
...However there is an ambiguity in the language grammar when using
<foo>
style assertions in JSX:
var foo = <string>bar;
</string>
Therefore it is now recommended that you just use
as foo
for consistency.