gpt4 book ai didi

typescript - 为什么 typescript 中有两种不同的转换语法?

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:08 25 4
gpt4 key购买 nike

据我所知,在 Typescript 中有两种类型的语法。

  1. 仅使用 <>类型转换

    const a = <A>b;
  2. 使用 as要投的声明

    const a = b as A;

我猜 1 或 2 的生成代码完全相同。而且,没有这样的语法可以使用 as我开始使用 Typescript 时的声明。我猜 as是 typescript 中的新语法。

但是,应该有一些理由来制定新的语法。为什么他们需要这些语法?如果只有 1 个 cast 语法,会有什么不方便吗?

最佳答案

如所述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 - 为什么 typescript 中有两种不同的转换语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38929330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com