1.0.1 • Published 7 months ago

nullthrows-es v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

nullthrows-es

An idiomatic way to enforce values not to be null or undefined, with first-class support for TypeScript. Very lightweight with no dependencies. This package is a fork of @expo/nullthrows and outputs its build as ES Modules.

Installation and usage

npm install nullthrows-es
import process from 'node:process';
import nullthrows from 'nullthrows-es';

// nodeEnv is guaranteed to be a string. If the environment variable is
// undefined, nullthrows throws a TypeError.
const nodeEnv = nullthrows(process.env.NODE_ENV);

// You can optionally provide a custom error message
const nodeEnv2 = nullthrows(process.env.NODE_ENV, `NODE_ENV must be defined`);

API

function nullthrows<T>(value: T | null | undefined, message?: string): NonNullable<T>

Parameters

  • value: the value to enforce not to be null or undefined
  • message: an optional error message to use in the thrown TypeError if the given value is null or undefined

Type parameters

  • T: the static type of value

Return value

Returns value if it is neither null nor undefined.

Throws

Throws a TypeError if value is null or undefined.

History

This package is forked from @expo/nullthrows.

1.0.1

7 months ago

1.0.0

7 months ago