Skip to content

A simple `unwrap` function for use with TypeScript

Notifications You must be signed in to change notification settings

LinusU/ts-unwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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