/typesafe-get

Safely drill for nested values

Primary LanguageTypeScript

typesafe-get

Safely drill for nested values

Install

  yarn add typesafe-get

Usage

import get from "ts-typesafe-get";

const obj = { a: 1, b: 2 };
const undef = undefined;
expect(get(() => obj.b, -1)).toEqual(2);
expect(get(() => obj.c, -1)).toEqual(-1);
expect(get(() => obj.a, -1)).toEqual(1);
expect(get(() => undef.a[0].b, -1)).toEqual(-1);
  

API

Signature :

function get<O>(
  reducer: () => O,
  defaultTo: Exclude<O, undefined | null>
): Exclude<O, undefined | null>;