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โ
| Value | Result |
|---|---|
0 | true |
'' | true |
false | true |
NaN | true |
null | false |
undefined | false |
Comparisonโ
| Guard | Excludes |
|---|---|
isNonNull | null |
isNonUndefined | undefined |
isNonNullable | null 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)
true
false