Skip to main content

Function: filterNullAndUndefined()

filterNullAndUndefined<TValue>(value: Nullish | TValue): value is TValue

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

Type parameters

Type parameter
TValue

Parameters

ParameterTypeDescription
valueNullish | TValueThe value to verify that is neither null nor undefined

Returns

value is TValue

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

Example

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

// TypeScript Type: string[]
const filteredArray = someArray.filter(filterNullAndUndefined);
// Result: ['one', 'two', 'five']

Source

lib/filterNullAndUndefined.ts:19