What is the usefulness of the exclamation (non null assertion Operator) in Typescript?

Asked

Viewed 745 times

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

1 answer

7


What you’re talking about in C# is operator ?. (since C# 6) and not type annotation ? which already exists since C# 2 for types by value and now exists in C# 8 for types by reference. C# will also have the operator of dammit (!) and it will indicate that you, the programmer, ensures to the compiler that that value will not be null.

Typescript works analogously, including C# was inspired by it (both are from the same creator).

On TS many codes generate exactly the same font in Javascript since the biggest improvements in language compared to JS are precisely about being a typed language, so you can do operations with more confidence that everything is right. Then that information serves for the TS compiler to determine whether an operation is valid or not (which in JS would only know during execution), but after that that information may disappear from the target code. TS has annotations to give more robustness and this operator is one more of them, it is not to change the generated code, it is to give guarantees.

The operator (non-null assertion) in question serves for you to tell the compiler that you know that this operation is safe and will never be null. In some cases the TS compiler might think it could be null and because of this it would not compile without guards in the code (without being able to assess the code flow easily and determine whether it can guarantee non-nullity). This is a way for you programmer to take responsibility and the compiler lets it go, even if he’s almost sure this is wrong. Its use only makes sense in variables of annulable types (with annotation ? in the end), which is not your case, so it seems that does nothing, but also does not generate error.

What are the chances of someone highly skilled having put something useless into the language and what are the chances of the person using not understanding its functioning?

  • That it was not useless I was sure po, it just does not do what I thought it did :v

Browser other questions tagged

You are not signed in. Login or sign up in order to post.