Skip to main content

isNonNullable()

isNonNullable<T>(value): value is NonNullable<T>

Checks if a value is not null or undefined.

๐Ÿ’Ž Why is this a Hidden Gem?

A powerful utility that filters out both null and undefined, narrowing types effectively.

note

Narrows T to NonNullable<T>. Uses loose equality (!= null) to check both.


Type Parametersโ€‹

T: Tโ€‹

The type of the value.


Parametersโ€‹

value: Tโ€‹

The value to check.


Returns: value is NonNullable<T>โ€‹

true if the value is not null or undefined, false otherwise.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Exampleโ€‹

isNonNullable('hello');   // => true
isNonNullable(0); // => true
isNonNullable(false); // => true
isNonNullable(null); // => false
isNonNullable(undefined); // => false

How it works?โ€‹

Type guard that checks if a value is not null or undefined.

Type Narrowingโ€‹

Common Checksโ€‹

ValueResult
0checkmark true
''checkmark true
falsecheckmark true
NaNcheckmark true
nullcross false
undefinedcross false

Comparisonโ€‹

GuardExcludes
isNonNullnull
isNonUndefinedundefined
isNonNullablenull AND undefined

Use Casesโ€‹

Filter missing values ๐Ÿ“Œโ€‹

Remove null and undefined from arrays while narrowing types. Perfect for cleaning data lists before processing.

const items = [1, null, 2, undefined, 3];
const cleanItems = items.filter(isNonNullable); // Type is number[]

Chain with optional accessโ€‹

Combine with optional chaining for clean null-safe pipelines.

const userNames = users
.map(u => u.profile?.displayName)
.filter(isNonNullable);
// Type: string[] (no undefined)