1

I was reading through some Angular documentation and the following code has stumped me. Specifically, there is a a call to a method named resolve, but sandwiched between the parenthesis and the method name is a "!".

clicked() {
    if (this.arrived) {
      this.reset();
    } else {
      this.resolve!('hi there!'); \\<-- Here?
      this.arrived = true;
    }
}

I've never seen this before. Why is there a bang after the method name? I've tried to search for this and I can't find an answer. Also, I'm not sure if this is a TypeScript or JavaScript thing.

For reference, the Angular reference document is the page on the AsyncPipe object. The code is in the second to last code snippet, close to the bottom of the snippet. What is this code doing?

isherwood
  • 58,414
  • 16
  • 114
  • 157
RLH
  • 15,230
  • 22
  • 98
  • 182
  • 1
    Yeah, this is a duplicate. The example the code above comes from has the `resolve` property defined as type `Function | null`. Presumably if `this.arrived` is `true`, then `this.resolve` will be defined. The compiler won't know about this dependency, so it would still complain that `this.resolve('hi there')` might be an error because `this.resolve` might be `null`. The non-null assertion in `this.resolve!` says to the compiler that `this.resolve` should be treated as `Function` and not `Function | null` in that expression. – jcalz Oct 15 '20 at 14:18
  • It is called "definite assignment assertion" and [has been introduced in TypeScript 2.7](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#definite-assignment-assertions) (January 2018). – axiac Oct 15 '20 at 14:25

0 Answers0