You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 --strictNullChecksfunctionvalidateEntity(e: Entity?){// Throw exception if e is null or invalid entity}functionprocessEntity(e: Entity?){validateEntity(e);lets=e!.name;// Assert that e is non-null and access name}
I wonder if we can introduce a similar semantics with JSDoc, something like
vare=/** ! */(x);
or
vare=/** @notnull */(x);
The text was updated successfully, but these errors were encountered:
I wouldn't rush to add this, especially since we don't know yet how popular it will be in TypeScript. We can get similar behavior by casting to an appropriate non-nullable type, or by using an assertion. Closing for now, and if in the future we find that it's a great convenience to have this operator, we can revisit.
The best way to get us to reconsider it would be to file a new issue since this is quite an old one.
That way it will get into our current issue triaging process.
At first glance, I can't see a strong reason to add this feature, since you can get this effect by specifying the actual type like this.
vare=/** @type {!ActualType} */(x);
So, if you file another issue, be sure to explain why the approach above isn't good enough.
I'm afraid that "it's too verbose" isn't likely to be a sufficient reason.
We actually prefer it when types are very explicitly specified, since that makes it easier for both the human reader and the compiler to know what is expected.
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.
I wonder if we can introduce a similar semantics with JSDoc, something like
or
The text was updated successfully, but these errors were encountered: