在打字稿中,是什么! (惊叹号/ bang)运算符取消引用成员时?

本文翻译自:In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?

When looking at the sourcecode for a tslint rule, I came across the following statement: 在查看tslint规则的源代码时,我遇到了以下语句:

if (node.parent!.kind === ts.SyntaxKind.ObjectLiteralExpression) {
    return;
}

Notice the ! 注意! operator after node.parent . node.parent之后的运算符。 Interesting! 有趣!

I first tried compiling the file locally with my currently installed version of TS (1.5.3). 我首先尝试使用当前安装的TS(1.5.3)版本在本地编译文件。 The resulting error pointed to the exact location of the bang: 产生的错误指出了爆炸的确切位置:

$ tsc --noImplicitAny memberAccessRule.ts 
noPublicModifierRule.ts(57,24): error TS1005: ')' expected.

Next I upgraded to the latest TS (2.1.6), which compiled it without issue. 接下来,我升级到了最新的TS(2.1.6),可以毫无问题地对其进行编译。 So it seems to be feature of TS 2.x. 因此,这似乎是TS 2.x的功能。 But the transpilation ignored the bang completely, resulting in the following JS: 但是编译完全忽略了爆炸,导致出现以下JS:

if (node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
    return;
}

My Google fu has thus far failed me. 到目前为止,我的Google赋使我失败了。

What is TS's exclamation mark operator, and how does it work? TS的感叹号运算符是什么,它如何工作?


#1楼

参考:https://stackoom.com/question/2rNML/在打字稿中-是什么-惊叹号-bang-运算符取消引用成员时


#2楼

That's the non-null assertion operator. 那就是非null断言运算符。 It is a way to tell the compiler "this expression cannot be null or undefined here, so don't complain about the possibility of it being null or undefined ." 这是一种告诉编译器“此表达式不能为nullundefined ,因此不要抱怨它为nullundefined的可能性”。 Sometimes the type checker is unable to make that determination itself. 有时类型检查器本身无法确定。

It is explained here : 在这里解释:

A new ! 一个新的! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. 在类型检查器无法得出结论的情况下,可以使用后缀表达式运算符来断言其操作数是非null且未定义的。 Specifically, the operation x! 具体来说,就是操作x! produces a value of the type of x with null and undefined excluded. 生成x类型的值,其中排除了nullundefined Similar to type assertions of the forms <T>x and x as T , the ! 与形式为<T>xx as T类型断言类似, x as T ! non-null assertion operator is simply removed in the emitted JavaScript code. 只需在发出的JavaScript代码中删除非null断言运算符即可。

I find the use of the term "assert" a bit misleading in that explanation. 在该说明中,我发现使用“断言”一词有点误导。 It is "assert" in the sense that the developer is asserting it , not in the sense that a test is going to be performed. 在开发人员声明它的意义上,这是“断言”,而不是在将要执行测试的意义上。 The last line indeed indicates that it results in no JavaScript code being emitted. 最后一行确实表明它没有发出JavaScript代码。


#3楼

Louis' answer is great, but I thought I would try to sum it up succinctly: 路易的回答很好,但我想我会简要总结一下:

The bang operator tells the compiler to temporarily relax the "not null" constraint that it might otherwise demand. bang运算符告诉编译器暂时放宽其可能要求的“非空”约束。 It says to the compiler: "As the developer, I know better than you that this variable cannot be null right now". 它对编译器说:“作为开发人员,我比您更清楚此变量现在不能为null”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值