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
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
... In regular type checking mode ... 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 unable to conclude that fact....
🌐
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 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.
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.

🌐
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.
🌐
Medium
medium.com › @tar.viturawong › a-note-on-typescript-non-null-assertion-operator-bad8fbe62bba
A note on TypeScript non-null assertion operator | by Tar Viturawong | Medium
March 25, 2019 - The assertion used in TypeScript’s release notes is an example of such a case. There is plenty of documentation why this is safe that is understandable by all. Often, if this is convoluted, it’s better to self-document with an explicit type assertion. ... It is absolutely clear where the scope of your knowledge/assumption about the safety of ignoring null ...
Top answer
1 of 1
9

This is mostly a terminology problem:

  • null and undefined are different, even though some parts of the language treat them similarly. (For example, the non-null assertion operator eliminates both null and undefined from the domain of the expression it operates on.)

  • The ! after a class property declaration is the definite assignment assertion operator operator, not the non-null assertion operator. (They are both written with a postfix !, but a non-null assertion appears after an expression, while a definite assignment assertion appears after a variable/property declaration.) A definite assignment assertion tells the compiler that it does not need to verify that a variable or property has been initialized before use. The definite assignment assertion operator has nothing to do with null.

If you do not initialize a property or variable, its value will be undefined, not null, if you read from it. If you have the --strictPropertyInitialization compiler option enabled (or just --strict, which includes it), and you have a class property whose type does not include undefined (not null), then you must either initialize it immediately on declaration, initialize it unconditionally inside the constructor, or use a definite assignment assertion:

class Example {
    a: string | undefined; // okay: includes undefined
    b: string | null = "b"; // okay: initialized
    c: string | null; // okay: assigned in constructor
    d: string | null; // error: compiler cannot be sure it is assigned
    e!: string | null; // okay: asserted as assigned

    constructor() {
        this.c = "c";
        if (Math.random() < 1000) {
            this.d = "d"
            this.e = "e";
        }
    }
}

Playground link to code

Find elsewhere
🌐
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.
🌐
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
It allows you to override TypeScript's type checking and assert that a variable or property will have a non-null value at runtime. ... function greet(name: string | null): string { // Use the non-null assertion operator to assert that 'name' ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-non-null-assertion-operator-postfix-type
TypeScript Non-null Assertion Operator (Postfix !) Type - GeeksforGeeks
April 28, 2025 - We use the non-null assertion operator ! to assert that the email property is non-null when accessing it. This is done because TypeScript considers user.email to be potentially undefined due to the email? property definition in the User type.
🌐
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.
🌐
Palantir
palantir.github.io › tslint › rules › no-non-null-assertion
Rule: no-non-null-assertion
Using non-null assertion cancels the benefits of the strict null checking mode. ... Not configurable.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
Reminder: Because type assertions ... be an exception or null generated if the type assertion is wrong. TypeScript only allows type assertions which convert to a more specific or less specific version of a type....
🌐
DEV Community
dev.to › audreyk › when-to-use-the-non-null-assertion-operator-in-typescript-545f
🧭 🇹 When to use the non-null assertion operator in TypeScript - DEV Community
August 18, 2024 - The non-null assertion operator (!) is used to assert that a value is neither null nor undefined. It tells TypeScript's type checker to ignore the possibility that the value is null or undefined, thus bypassing type checking for these values.
🌐
Omarileon
omarileon.me › blog › typescript-non-null-assertion
mari. | Typescript Non-Null Assertions: That Weird Exclamation Mark Operator
February 2, 2024 - The non-null assertion operator, or that weird exclamation mark you might be seeing in TypeScript is an operator that tells TypeScript that a value won't be null or undefined. In a technical sense, it eliminates null and undefined from the type ...
🌐
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