typescript 在打字稿中,什么是!(感叹号/爆炸)取消引用成员时的运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42273853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
提问by Mike Chamberlain
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
. Interesting!
注意!
后面的操作符node.parent
。有趣的!
I first tried compiling the file locally with my currently installed version of TS (1.5.3). The resulting error pointed to the exact location of the bang:
我首先尝试使用当前安装的 TS (1.5.3) 版本在本地编译该文件。由此产生的错误指向了爆炸的确切位置:
$ 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. So it seems to be feature of TS 2.x. Butthe transpilation ignored the bang completely, resulting in the following JS:
接下来我升级到最新的 TS (2.1.6),编译没有问题。所以它似乎是 TS 2.x 的特性。但是转译完全忽略了 bang,导致以下 JS:
if (node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
return;
}
My Google fu has thus far failed me.
到目前为止,我的 Google fu 让我失望了。
What is TS's exclamation mark operator, and how does it work?
什么是TS的感叹号运算符,它是如何工作的?
回答by Louis
That's the non-null assertion operator. 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
." Sometimes the type checker is unable to make that determination itself.
那是非空断言运算符。这是一种告诉编译器“这个表达式不能是null
or 的方式undefined
,所以不要抱怨它是null
or的可能性undefined
。” 有时,类型检查器本身无法做出决定。
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. Specifically, the operationx!
produces a value of the type ofx
withnull
andundefined
excluded. Similar to type assertions of the forms<T>x
andx as T
, the!
non-null assertion operator is simply removed in the emitted JavaScript code.
!
在类型检查器无法得出该事实的上下文中,可以使用新的后缀表达式运算符来断言其操作数为非空和非未定义。具体来说,该操作x!
会产生一个类型为x
withnull
和undefined
exclude 的值。与表单<T>x
and 的类型断言类似x as T
,!
非空断言运算符在发出的 JavaScript 代码中被简单地删除。
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 代码。
回答by Mike Chamberlain
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. It says to the compiler: "As the developer, I know better than you that this variable cannot be null right now".
bang 运算符告诉编译器暂时放宽它可能需要的“非空”约束。它对编译器说:“作为开发人员,我比你更清楚这个变量现在不能为空”。