Skip to content

isNotNil() function

Checks if the given value is not null or undefined.

The opposite of isNil. As a type guard it narrows T | null | undefined to NonNullable<T>, which makes it useful with filter.

Signature:

typescript
isNotNil: <T>(input: T) => input is NonNullable<T>

Example

ts
isNotNil(1); // true
isNotNil("1"); // true
isNotNil(undefined); // false
isNotNil(null); // false

// with filter
pipe(
  [1, null, 2, undefined, 3],
  filter(isNotNil),
  toArray,
); // [1, 2, 3] - narrowed to number[]

Open Source Code

Released under the Apache-2.0 License.