我有一个对象X
和一个方法getY()
,返回一个对象Y
和一个方法a()
,在typescript中.
X.getY()!.a()
我猜!
运算符是用来判断null的,但它具体是如何工作的呢?语言的定义在哪里?
我有一个对象X
和一个方法getY()
,返回一个对象Y
和一个方法a()
,在typescript中.
X.getY()!.a()
我猜!
运算符是用来判断null的,但它具体是如何工作的呢?语言的定义在哪里?
它被称为"非空断言运算符",它告诉编译器x.getY()
不是空的.
It's a new typescript 2.0 feature and you can read about it in the what's new page, here's what it says:
一个新的!post fix表达式运算符可用于断言其
// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
// Throw exception if e is null or invalid entity
}
function processEntity(e?: Entity) {
validateEntity(e);
let s = e!.name; // Assert that e is non-null and access name
}
There's an issue for documenting this feature: Document non-null assertion operator (!)