Closed
Description
TypeScript is implementing non-nullable types and strict null checks in microsoft/TypeScript#7140.
I'm interested in one thing they are adding to the language:
Non-null assertion operator
A new ! postfix 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 operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.
// 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
}
I wonder if we can introduce a similar semantics with JSDoc, something like
var e = /** ! */ (x);
or
var e = /** @notnull */ (x);