es-toolkit/docs/reference/predicate/isNotNil.md
Minsoo Kim cf65b2c601
style(*): Setup prettier and apply formatting (#24)
* chore: add prettierrc

* chore: apply format with prettier config

* chore: eslint error fix
2024-06-04 17:19:26 +09:00

689 B

isNotNil

Checks if the given value is not null nor undefined.

This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to ensure it is not nullable.

Signature

function isNotNil<T>(x: T | null | undefined): x is T;

Parameters

  • x (T | null | undefined): The value to test if it is not null nor undefined.

Returns

(x is T): True if the value is not null nor undefined, false otherwise.

Examples

// Here the type of `arr` is (number | undefined)[]
const arr = [1, undefined, 3];
// Here the type of `result` is number[]
const result = arr.filter(isNotNil);
// result will be [1, 3]