我猜JavaScript中存在的操作符使用了它们原来的优先级,但是新的构造,比如类型断言(即<Type> expr
)和箭头函数表达式就不那么清晰了。
如果在某个地方有一张正式的桌子会很有帮助。
发布于 2015-02-04 00:42:08
我在下面提供了一些Type断言的例子,希望能为您提供一些关于这个特性的信息。
有趣的一点是,如果一组括号对类型断言的范围有影响,则在编译期间通过类型擦除删除括号。这意味着下面的示例b
会导致JavaScript中的(x)
,而示例c
只会导致x
。
使用括号设置类型断言的作用域的唯一原因是,当您希望应用到右侧表达式的一部分时,在这种情况下,您将类型断言放在括号内,如示例e
所示。
interface SomeType {
name: string;
id: number;
}
var x = {};
var a: SomeType = <SomeType> x;
// Note, when brackets do not affect
// type assertions they will be left in the output
var b: SomeType = <SomeType> (x);
// When brackets affect type assertions,
// they are erased along with the type information
var c: SomeType = (<SomeType> x);
// Two errors:
// - SomeType is not assignable to d
// - Property id does not exist on x
var d: number = <SomeType> x.id;
// Brackets are useful here:
var e: number = (<SomeType> x).id;
// More complex example
var func = () => {
return x; // return type is {}
};
var f: SomeType = <SomeType> func();
https://stackoverflow.com/questions/28303725
复制