108

I've seen in some React/TypeScript implementations such as :

ref={ ref => this.container = ref! }

What does the exclamation point means in ref!? Is that something specific in TypeScript, or a new standard JavaScript notation?

Bharata
  • 13,509
  • 6
  • 36
  • 50
dbrrt
  • 2,275
  • 4
  • 18
  • 35

1 Answers1

202

In TypeScript, a postfix ! removes null and undefined from the type of an expression.

This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null or undefined actually isn't.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • 54
    For reference: this is called the [non-null assertion operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator). – Mattias Buelens Dec 07 '17 at 22:14