ts-unwrap
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Unwrap for TypeScript

A simple unwrap function for use with TypeScript.

Installation

npm install --save ts-unwrap

Usage

import unwrap from 'ts-unwrap'

// `port` is of type string, and will never be undefined
const port = unwrap(process.env['PORT'])

// optionally specify a custom error message
const port = unwrap(process.env['PORT'], 'Missing PORT from environment')

API

unwrap<T>(value: T | undefined | null, errorMessage?: string): T

Throws an error if the value is undefined or null, otherwise it returns the value.

The description of the thrown error will be 'Missing value' unless another message is passed as the errorMessage argument.

Related Packages

Prior Art

Readme

Keywords

none

Package Sidebar

Install

npm i ts-unwrap

Weekly Downloads

182

Version

2.0.0

License

MIT

Unpacked Size

1.99 kB

Total Files

5

Last publish

Collaborators

  • linusu