Using a juggling-check, you can test both null and undefined in one hit:

if (x == null) {

If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:

if (x === null) {

You can try this with various values using this example:

var a: number;
var b: number = null;

function check(x, name) {
    if (x == null) {
        console.log(name + ' == null');
    }

    if (x === null) {
        console.log(name + ' === null');
    }

    if (typeof x === 'undefined') {
        console.log(name + ' is undefined');
    }
}

check(a, 'a');
check(b, 'b');

Output

"a == null"

"a is undefined"

"b == null"

"b === null"

Answer from Fenton on Stack Overflow
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
When strictNullChecks is enabled, TypeScript requires values to be set unless undefined is explicitly added to the type. Optional chaining is a JavaScript feature that works well with TypeScript's null handling. It allows accessing properties on an object that may or may not exist, using compact syntax.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
Non-null and non-undefined type guards may use the ==, !=, ===, or !== operator to compare to null or undefined, as in x != null or x === undefined. The effects on subject variable types accurately reflect JavaScript semantics (e.g. double-equals operators check for both values no matter which one is specified whereas triple-equals only checks for the specified value).
🌐
Omarileon
omarileon.me › blog › typescript-null-undefined
mari. | How to Detect Null and Undefined in Your TypeScript Code
February 27, 2024 - Another way to check for null or undefined is to use the nullish coalescing operator (??), which was introduced in TypeScript 3.7. If the left-hand side of the operation is non-null it returns that, otherwise it returns the right-hand side otherwise.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-check-null-and-undefined-in-typescript
How to check null and undefined in TypeScript ? - GeeksforGeeks
July 23, 2025 - We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
🌐
Zipy
zipy.ai › blog › debug-typescript-null-or-undefined-value-errors
Solving Typescript Null & Undefined Errors: Expert Tips
February 28, 2024 - This design choice is inherited from JavaScript, providing flexibility in indicating the absence of a value (null) versus an uninitialized variable (undefined). ... Zipy offers proactive error monitoring and session replay capabilities, allowing developers to see exactly what led to an error, including the user's actions and the application state at the time of the error. Understand TypeScript's Type System: Leveraging TypeScript's strict typing can prevent many Null or Undefined Value Errors.
🌐
Rampatra
blog.rampatra.com › null-vs-undefined-in-typescript-or-javascript-how-to-check-for-both-at-once
!== null vs !== undefined in Typescript or Javascript, how to check for both at once?
if (value != null) { // Do something if value is not null or undefined } Prefer strict equality checks (!== or ===) for better type safety and clarity.
🌐
Reddit
reddit.com › r/typescript › undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

Since switching to TypeScript I have been using a lot of optional properties, for example:

type store = {
  currentUserId?: string
}

function logout () {
  store.currentUserId = undefined
}

However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:

type store = {
  currentUserId: string | null
}

function logout () {
  store.currentUserId = null
}

It seems like the use of undefined in TypeScript differs slightly from in Javascript.

Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?

Find elsewhere
🌐
GitBook
basarat.gitbook.io › typescript › recap › null-undefined
Null vs. Undefined | TypeScript Deep Dive
So to check if a variable is defined or not at a global level you normally use typeof: ... if (typeof someglobal !== 'undefined') { // someglobal is now safe to use console.log(someglobal); } Because TypeScript gives you the opportunity to document your structures separately from values instead of stuff like: ... Node style callback functions (e.g. (err,somethingElse)=>{ /* something */ }) are generally called with err set to null if there isn't an error.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null vs undefined in typescript
Null Vs Undefined in TypeScript - Tektutorialshub
March 15, 2023 - But they are neither false nor true. You can use typeof operator to check for undefined but not null as it returns “object”. You can use the == & === operator to check their values · Checking for null.
🌐
Upmostly
upmostly.com › home › typescript › how to detect null and undefined
How to Detect Null and Undefined in Your TypeScript Code - Upmostly
March 28, 2023 - Another way to check for null or undefined is to use the nullish coalescing operator (??), which was introduced in TypeScript 3.7.
🌐
Angular Wiki
angularjswiki.com › angular › how-to-check-both-null-or-undefined-in-typescript-or-angular
How To Check Both null or undefined in TypeScript/Angular | Angular Wiki
different ways we can check both null or undefined in TypeScript or Angular. 1. By using simple if condition 2. By using TypeScript Nullish Coalescing & Optional chaining. 3. By using Array.include() function.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-check-if-null
How to correctly check for Null in TypeScript | bobbyhadz
The first if statement uses the loose equals (==) operator instead of strict equals (===), and checks if the variable is equal to null or undefined. This checks for both null and undefined because when using loose equals (==), null is equal ...
🌐
TutorialsPoint
tutorialspoint.com › javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator.
🌐
Honlsoft
honlsoft.com › blog › 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
A very common use case is to use an alternate default for null or undefined. console.log(null ?? "hello") // Outputs: hello console.log(undefined ?? "hello") // Outputs: hello console.log("world" ?? "hello") // Outputs: world console.log("" ?? "hello") // Outputs: "" console.log(0 ?? "hello") // Outputs: 0 · The huge difference here is it is just checking for null or undefined.
Top answer
1 of 2
2

I know this post is a little bit old, but now i think you can simply write something like

/**
 * Generic type definition that describes a 'not undefined' type.
 */
type Defined<T> = T extends undefined ? never : T

/**
 * Generic type guard function for values runtime-checked as defined.
 *
 * @param argument - The argument to check.
 *
 * @returns The boolean describing the assertion.
 * @remarks Uses the {@link Defined} type as returned value.
 */
function isDefined<T>(argument: T): argument is Defined<T> {
  return !!argument
}

const foo: string | undefined
isDefined(foo)
      ? foo //string
      : foo //undefined (not string | undefined anymore)
2 of 2
0

Okay, I found the solution that are works and types-friendly enough:

tl;dr

function isDefined<T>(value: T | undefined | null): value is T {
  return <T>value !== undefined && <T>value !== null;
}

Why?

In this way isDefined() will respect variable's type and the following code would know take this check in account.

Example 1 - basik check:

function getFoo(foo: string): void { 
  //
}

function getBar(bar: string| undefined) {   
  getFoo(bar); //ERROR: "bar" can be undefined
  if (isDefined(bar)) {
    getFoo(bar); // Ok now, typescript knows that "bar' is defined
  }
}

Example 2 - types respect:

function getFoo(foo: string): void { 
  //
}

function getBar(bar: number | undefined) {
  getFoo(bar); // ERROR: "number | undefined" is not assignable to "string"
  if (isDefined(bar)) {
    getFoo(bar); // ERROR: "number" is not assignable to "string", but it's ok - we know it's number
  }
}
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
In TypeScript 3.7, this is identified as a likely error: ... This check is a breaking change, but for that reason the checks are very conservative. This error is only issued in if conditions, and it is not issued on optional properties, if strictNullChecks is off, or if the function is later called within the body of the if: ... If you intended to test the function without calling it, you can correct the definition of it to include undefined/null...
🌐
Amit Merchant
amitmerchant.com › strictly-check-null-undefined-typescript
Strictly check for null and undefined values in TypeScript
May 27, 2020 - Upon adding this option, TypeScript’s compiler will start showing typing errors because null and undefined are no longer valid values for any type. So, for the above example, it will show the following error. Type 'null' is not assignable to type 'string'. To fix our appendDomain function, we can add more types to the arguments to include null and undefined like so and checking if the provided value in the function is of type string explicitly like so.
🌐
Scaler
scaler.com › topics › typescript › strictnullchecks
StrictNullChecks in TypeScript - Scaler Topics
February 8, 2023 - Because nulls are permitted, the TypeScript compiler believes that the p might be null. There are many ways to solve the above error, out of which a few good ones are : ... Use if to check for null : If the condition is null or undefined.