typescript JavaScript 中变量后面的感叹号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47704652/
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
What does exclamation point after variable mean in JavaScript?
提问by dbrrt
I've seen in some React/TypeScript implementations such as :
我在一些 React/TypeScript 实现中看到过,例如:
ref={ ref => this.container = ref! }
ref={ ref => this.container = ref! }
What does the exclamation point means in ref!
?
Is that something specific in TypeScript, or a new standard JavaScript notation?
感叹号是什么意思ref!
?这是 TypeScript 中的特定内容,还是新的标准 JavaScript 符号?
回答by Ryan Cavanaugh
In TypeScript, a postfix !
removes null
and undefined
from the type of an expression.
在 TypeScript 中,后缀从表达式的类型中!
删除null
和undefined
。
This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null
or undefined
actually isn't.
当您出于 TypeScript 推理能力之外的原因知道一个“可能”null
或undefined
实际上不是的变量时,这很有用。