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
We can, of course, use a type assertion to resolve the type error. However, the non-null assertion operator is a more concise solution to type errors that involve null or undefined. It is worth noting that if the code were more straightforward, then TypeScript would understand that text on the return statement wasn't null.
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.

Discussions

Is "as" any better than "!" to assert non-nullishness?
If I had to choose one, I'd use the non-null assertion operator (!) because it only removes null and undefined from the type. If req.user ever becomes anything other than Express.User, as Express.User will potentially silently override that change.A pretty useless example: type T = Date | undefined; const getDateString = (date: T) => {return (date as Date).toISOString();}; Say we wanted this to support a timestamp as well, so we change type T = Date | number | undefined.This will not error: return (date as Date).toISOString();, but this will: return date!.toISOString(); Like stable_orange_genius said, I'd disable the rule, I'm not sure why it's recommended either. More on reddit.com
๐ŸŒ r/typescript
105
28
March 15, 2023
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
typescript - Non-null assertion in class property - Stack Overflow
I am fairly new to typescript and have scoured the web trying to find an explanation for this. Recently I have been working on a project and have wanting to use sequelize with it. When reading thro... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Non-null assertion operator vs. Optional chaining
Non null assertion is fundamentally different than optional chaining. Non null assertion will not null guard your property chain at run time, it will instead tell the transport that the value preceding the operator will never be null. If it happens to be null, your program will crash. Optional chaining will null check the property before it before continuing down the property chain, essentially guarding that value against being null or undefined. More on reddit.com
๐ŸŒ r/typescript
2
7
October 24, 2019
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ cleaner-typescript-with-the-non-null-assertion-operator-300789388376
Cleaner TypeScript With the Non-Null Assertion Operator
February 16, 2021 - The non-null assertion operator takes a typed variable and removes the undefined and null types from it.
๐ŸŒ
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 begins and ends.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
June 15, 2024 - 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.
๐ŸŒ
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
November 19, 2022 - 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.
๐ŸŒ
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
function greet(name: string | null): string { // Use the non-null assertion operator to assert that 'name' is not null return `Hello, ${name!.toUpperCase()}!`; } console.log(greet("Alice")); // Output: Hello, ALICE!
๐ŸŒ
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 ...
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

๐ŸŒ
TypeScript
typescriptlang.org โ€บ docs โ€บ handbook โ€บ release-notes โ€บ typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
In regular type checking mode the inferred type of z is any because of widening, but in strict null checking mode the inferred type 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 unable to conclude that fact.
๐ŸŒ
W3Schools
w3schools.com โ€บ typescript โ€บ typescript_null.php
TypeScript Null & Undefined
Optional chaining is a JavaScript feature that works well with TypeScript's null handling.
๐ŸŒ
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 ...
๐ŸŒ
TypeScript
typescriptlang.org โ€บ docs โ€บ handbook โ€บ 2 โ€บ everyday-types.html
TypeScript: Documentation - Everyday Types
Reminder: Because type assertions are removed at compile-time, there is no runtime checking associated with a type assertion. There wonโ€™t 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.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ understanding the exclamation mark in typescript
Understanding the exclamation mark in TypeScript - LogRocket Blog
June 4, 2024 - The ! operator does not change the runtime behavior of your code. If the value you have asserted is not null or undefined turns out to actually be null or undefined, an error will occur and disrupt the execution of your code. Remember, the difference between TypeScript and JavaScript is the assertion of types.
๐ŸŒ
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.