It's called the "Non-null assertion operator" and it tells the compiler that x.getY() is not null.

It's a new typescript 2.0 feature and you can read about it in the what's new page, here's what it says:

A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.

// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
    // Throw exception if e is null or invalid entity
}

function processEntity(e?: Entity) {
    validateEntity(e);
    let s = e!.name;  // Assert that e is non-null and access name
}

Edit

There's an issue for documenting this feature: Document non-null assertion operator (!)

Answer from Nitzan Tomer on Stack Overflow
๐ŸŒ
Learn TypeScript
learntypescript.dev โ€บ 07 โ€บ l2-non-null-assertion-operator
Using the non-null assertion operator | Learn TypeScript
The non-null assertion operator is an exclamation mark (!), and this is placed after the variable or expression that we want to tell TypeScript isn't null or undefined.
๐ŸŒ
TypeScript
typescriptlang.org โ€บ docs โ€บ handbook โ€บ release-notes โ€บ typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
In regular type checking mode the ... of z is null (and therefore, absent a type annotation, null is the only possible value for z). A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is ...
Top answer
1 of 3
170

It's called the "Non-null assertion operator" and it tells the compiler that x.getY() is not null.

It's a new typescript 2.0 feature and you can read about it in the what's new page, here's what it says:

A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.

// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
    // Throw exception if e is null or invalid entity
}

function processEntity(e?: Entity) {
    validateEntity(e);
    let s = e!.name;  // Assert that e is non-null and access name
}

Edit

There's an issue for documenting this feature: Document non-null assertion operator (!)

2 of 3
45

Non-null assertion operator: !

  • You tells the TS compiler that the value of a variable is not null | undefined
  • Use it when you are in possession of knowledge that the TS compiler lacks.

Here is a trivial example of what it does:

let nullable1: null | number;
let nullable2: undefined | string;

let foo  = nullable1! // type foo: number
let fooz = nullable2! // type fooz: string

It basically removes null | undefined from the type


When do I use this?

Typescript is already pretty good at inferring types for example using typeguards:

let nullable: null | number | undefined;

if (nullable) {
    const foo = nullable; // ts can infer that foo: number, since if statements checks this
}

However sometimes we are in a scenario which looks like the following:

type Nullable = null | number | undefined;

let nullable: Nullable;

validate(nullable);

// Here we say to ts compiler:
// I, the programmer have checked this and foo is not null or undefined
const foo = nullable!;  // foo: number

function validate(arg: Nullable) {
    // normally usually more complex validation logic
    // but now for an example
    if (!arg) {
        throw Error('validation failed')
    }
}

My personal advice is to try to avoid this operator whenever possible. Let the compiler do the job of statically checking your code. However there are scenarios especially with vendor code where using this operator is unavoidable.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ typescript-non-null-assertion-operator-postfix-type
TypeScript Non-null Assertion Operator (Postfix !) Type - GeeksforGeeks
April 28, 2025 - This operator tells TypeScript to treat the expression as if it's guaranteed to have a value, eliminating compile-time null and undefined checks. ... Type is the expected type of the value. value is the variable or expression that you're asserting ...
๐ŸŒ
Biome
biomejs.dev โ€บ linter โ€บ rules โ€บ no-non-null-assertion
noNonNullAssertion | Biome
TypeScriptโ€™s ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe.
Find elsewhere
๐ŸŒ
Kotlin
kotlinlang.org โ€บ docs โ€บ null-safety.html
Null safety | Kotlin Documentation
The not-null assertion operator !! converts any value to a non-nullable type.
๐ŸŒ
TypeScript ESlint
typescript-eslint.io โ€บ rules โ€บ no-non-null-assertion
no-non-null-assertion | typescript-eslint
TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ kotlin โ€บ kotlin basics โ€บ not-null assertion (!!) operator in kotlin
Not-Null Assertion (!!) Operator in Kotlin | Baeldung on Kotlin
March 19, 2024 - If we apply this operator on a null value, Kotlin will throw an instance of java.lang.NullPointerException: @Test fun givenNullableValue_WhenIsNull_ThenShouldThrow() { assertFailsWith<NullPointerException> { val noAnswer: String? = null noAnswer!! } } Also, if we apply !! to a non-nullable type, the compiler will still compile the code:
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ react โ€บ how to fix the forbidden non-null assertion in typescript and react?
How to fix the forbidden non-null assertion in TypeScript and React? | Sentry
The Non-null Assertion Operator (Postfix !) assertion removes the null and undefined types from a value. To use it, add the ! symbol after an expression like in the console log example above.
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ cleaner-typescript-with-the-non-null-assertion-operator-300789388376
Cleaner TypeScript With the Non-Null Assertion Operator | by Omri ...
February 16, 2021 - TL;DR: Adding an exclamation mark after a variable will ignore undefined or null types. The non-null assertion operator takes a typed variable and removes the undefined and null types from it.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 76232881 โ€บ how-to-get-rid-of-non-null-assertion
typescript - How to get rid of non-null assertion - Stack Overflow
In TypeScript, the non-null assertion operator (!) is used to tell the compiler that a value will not be null or undefined.
๐ŸŒ
TypeScript ESlint
typescript-eslint.io โ€บ rules โ€บ no-extra-non-null-assertion
no-extra-non-null-assertion | typescript-eslint
The ! non-null assertion operator in TypeScript is used to assert that a value's type does not include null or undefined.
๐ŸŒ
Nicotsou
nicotsou.com โ€บ tltr-typescript-non-null-assertion
Dealing With Non-Null Assertions in TypeScript
One of the most controversial features of TypeScript is definitely the Non-Null Assertion operator. It allows you to easily check for nullable values, without writing a lot of logic.
๐ŸŒ
SonarSource
rules.sonarsource.com โ€บ typescript โ€บ rspec-2966
Non-null assertions should not be used
Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your TYPESCRIPT code