As of TypeScript 2.1, you can use a lookup type to access an interface property.

IncomingMessage['url'] // string | undefined

You can combine that with NonNullable to fit your use case.

NonNullable<IncomingMessage['url']> // string

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html

Answer from Casey Woolfolk on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
In particular, the null and undefined ... the ! non-null assertion expression operator is permitted but has no effect in regular type checking mode. Thus, declaration files that are updated to use null- and undefined-aware types can still be used in regular type checking mode for backwards ...
🌐
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 - It turned out that this operator ! placed after an expression is TypeScript’s non-null assertion operator.
🌐
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.
🌐
Medium
medium.com › @gabrielairiart.gi › the-power-of-nonnullable-t-in-typescript-9cf156beb8da
The Power of NonNullable<T> in TypeScript | by Gabriela Iriart | Medium
March 28, 2024 - This is particularly useful in TypeScript's statically typed environment. NonNullable<T> is a utility type that excludes null and undefined from the set of allowed types for a given variable.
🌐
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.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 7648
Some way to express a non-null / non-undefined any would be nice · Issue #7648 · microsoft/TypeScript
February 18, 2016 - While updating the various .d.ts to have | null and | undefined, I came across some typings that use any but don't allow null or undefined. Object.defineProperty: /** * Adds a property to an object, or modifies attributes of an existing ...
Published   Mar 23, 2016
Find elsewhere
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.

🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-non-null-assertion-operator-postfix-type
TypeScript Non-null Assertion Operator (Postfix !) Type - ...
April 28, 2025 - We create a user object with a name property (non-nullable) and an email property (nullable). 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?
🌐
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.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
TypeScript has a powerful system to deal with null or undefined values.
🌐
Marius Schulz
mariusschulz.com › blog › non-nullable-types-in-typescript
Non-Nullable Types in TypeScript — Marius Schulz
July 11, 2019 - So how do we make a variable nullable in TypeScript 2.0? Since types are non-nullable by default when strict null checking is enabled, we need to explicitly opt into nullability and tell the type checker which variables we want to be nullable.
🌐
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.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-the-typescript-nonnullable-type-works-ccfd68972f02
How the TypeScript NonNullable Type Works | JavaScript in Plain English
July 22, 2024 - The NonNullable type is a utility type in TypeScript that creates a new type, whilst removing all null or undefined elements. It lets us take existing types, and modify them so they are more suitable in certain situations.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › nullable types / non nullable types in typescript
Nullable Types / Non Nullable Types in TypeScript - Tektutorialshub
March 15, 2023 - The compiler will not throw any error. But from the Typescript version 2.0, we can define Non Nullable types with the --strictNullChecks flag on. The strictNullChecks is flag is set to false by default.
🌐
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.