DEV Community

Discussion on: Typescript: Bang operator considered harmful

kapcash profile image
Florent Catiau-Tristant

It may be useful in some edge cases imo. For instance in this dummy example:

   product: Product | null = null;

   init() {
      if (!this.product) {
         this.product = { title: 'my product' }

   updateTitle() {
      this.title = this.product!.title;
Enter fullscreen mode Exit fullscreen mode

In the updateTitle method, you may not want to do the nullish check again and you know, from the context, that this.product cannot be null.
Here is the place to use the ! operator.

But in most cases, including this one (ones might add a non safe call to updateTitle), it's dangerous, I'll agree with that :)