6
I recently discovered that in Typescript we can use the operator of non null assertion simply putting a ! where you want to check. When I saw it, I thought it was like in C#, that we have the ?, that checks if the value is zero before proceeding with the operation. However, when doing some tests, it seems to me that it is simply useless.
I used a transpilator Typescript online with the following code:
var t = {
func: function () {
return 1;
}
};
var g = t!.func!();
And it generates me this code in Javascript:
var t = {
func: function () {
return 1;
}
};
var g = t.func();
Since I was hoping he wouldn’t make a mistake in case the property func is null or undefined, something like:
var g = t.func == null ? null : t.func();
However, if I take the !, it generates me the same code. With that my doubts are:
- What is the use since operator and when should I use it?
- On what occasions will it generate a different code?
I believe this check is only in transpilation time
– Costamilam