Skip to main content

Function: filterNullAndUndefinedAndZero()

filterNullAndUndefinedAndZero<TValue>(value: 0 | Nullish | TValue): value is TValue

Checks whether a value is not null nor undefined nor 0. This can be used in Array.filter to remove null, undefined from the array type

Type parameters

Type parameter
TValue

Parameters

ParameterTypeDescription
value0 | Nullish | TValueThe value to verify that is neither null, undefined nor 0

Returns

value is TValue

A boolean that is true if the value is neither null, undefined nor 0, false otherwise.

Example

// TypeScript Type: (string | number | undefined | null)[]
const someArray = ['one', 'two', undefined, null, 0, 1];

// TypeScript Type: (string | number)[]
const filteredArray = someArray.filter(filterNullAndUndefinedAndZero);
// Result: ['one', 'two', 1]

Source

lib/filterNullAndUndefinedAndZero.ts:19