变量中的感叹号在JavaScript中意味着什么?

dbr*_*rrt 15 javascript typescript

我在一些React/TypeScript实现中看到过,例如:

ref={ ref => this.container = ref! }

感叹号的含义是ref!什么?这是TypeScript中的特定内容,还是新的标准JavaScript表示法?

Rya*_*ugh 42

在打字稿,一个后缀!移除nullundefined从表达式的类型.

当您因为TypeScript的推理能力之外的原因而知道"可能" nullundefined实际上不是的变量时,这非常有用.

  • 供参考:这称为[非空断言运算符](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator). (9认同)