undefsafe-typed
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

undefsafe-typed

Build Status

Library to safely get subproperties of objects in typescript, without crashing if any child is undefined along the object tree. Inspired by undefsafe.

Fully supports types and inferres children types as return types.

Install:

yarn add undefsafe-typed

Usage:

interface IChild {
  name?: string;
  age?: number;
}
interface IMainObject {
  value?: string;
  children?: IChild[];
  someOtherNestedValue?: {
    a?: {
      b?: string;
    };
  };
}

const item = {
  name: 'item1',
  children: [{}, { name: 'John Doe', age: 21 }],
};

const name = undefsafe(item, 'name'); // name is inferred to string
const firstChildAge = undefsafe(item, 'children', 0, 'age'); // firstChildAge is inferred to number
const thirdChildAge = undefsafe(item, 'children', 3, 'age'); // thirdChildAge is inferred to number
const nestedB = undefsafe(item, 'someOtherNestedValue', 'a', 'b'); // is inferred to string

Dev and test

yarn install
yarn test

/undefsafe-typed/

    Package Sidebar

    Install

    npm i undefsafe-typed

    Weekly Downloads

    109

    Version

    1.0.2

    License

    ISC

    Unpacked Size

    136 kB

    Total Files

    5

    Last publish

    Collaborators

    • everreal