Skip to content
Generic options parameter handling
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Smart options handling, with one line of code:

  • throw detailed error on invalid options
  • set default values for missing options

Strongly-typed, built for TypeScript 3.x strict mode.

Build Status Coverage Status


  • Passing in invalid or misspelled option names is one of the most common errors.
  • Assigning defaults is the most common operation for methods that take options.

This module automates proper options parsing and setting defaults where needed.


$ npm install assert-options


import {assertOptions} from 'assert-options';

function functionWithOptions(options) {
    options = assertOptions(options, {first: 123, second: null});
    // options is a safe object here, with all missing defaults set.

And when default values are not needed, you can use an array of strings:

function functionWithOptions(options) {
    options = assertOptions(options, ['first', 'second']);
    // the result is exactly the same as using the following:
    // options = assertOptions(options, {first: undefined, second: undefined});
    // options is a safe object here, without defaults.


assertOptions(options, defaults) => {}

  • When options is null/undefined, new {} is returned, applying defaults as specified.

  • When options contains an unknown property, Error Option "name" is not recognized. is thrown.

  • When a property in options is missing or undefined, its value is set from the defaults, provided it is available and not undefined.

  • When options is not null/undefined, it must be of type object, or else TypeError is thrown: Invalid "options" parameter: value.

  • Parameter defaults is required, as a non-null object or an array of strings, or else TypeError is thrown: Invalid "defaults" parameter: value.

You can’t perform that action at this time.