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. Answer from _a_user_of_reddit_ on reddit.com
🌐
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.
🌐
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.
Discussions

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
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
what's the point to use non-null assertion?
I'm new to typescript and my textbook says: A non-null assertion should be used only when you know that a null value cannot occur. A runtime error will be caused if you apply the assertion and a n... More on stackoverflow.com
🌐 stackoverflow.com
! operator in typescript after object method - Stack Overflow
I have an object X with a method ... a(), in typescript. What does it mean an expression like this one: ... I guess the ! operator is used to check against null, but how does it work concretely? Where is defined in the language? ... It's called the "Non-null assertion operator" ... More on stackoverflow.com
🌐 stackoverflow.com
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

🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
Optional chaining is a JavaScript feature that works well with TypeScript's null handling.
🌐
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
🌐
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.
🌐
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.
🌐
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
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
We owe a large thanks to community ... out their pull request and the nullish coalescing proposal repository. ... There’s a specific set of functions that throw an error if something unexpected happened. They’re called “assertion” functions....
🌐
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 ...
🌐
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!
🌐
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 ...
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.

🌐
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.
🌐
Ohansemmanuel
blog.ohansemmanuel.com › in-typescript-what-is-the-exclamation-mark-or-bang-operator
In Typescript, what is the ! (exclamation mark / bang) operator?
July 7, 2022 - This is technically called the non-null assertion operator. If the typescript compiler complains that a value could be null or undefined, you can use the ! operator to assert that the said value is NOT null or undefined.
🌐
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.
🌐
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.