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

1.1.0 • Public • Published

ts-null-safe

safe deep get & set methods, with type safety.

NPM version Downloads

Install

Node.js

npm install ts-null-safe

Usage

import { NullSafe } from "ts-null-safe";

const obj = {
  a: {
    b: 123,
    c: ["e", "f"],
    d: undefined as { e: boolean } | undefined,
  }
};

NullSafe.get(obj, 'a', 'b'); // returns 123 as number | null | undefined
NullSafe.get(obj, 'a', 'c', 0); // returns 'e' as string | null | undefined
NullSafe.get(obj, 'a', 'd', 'e'); // returns undefined as boolean | null | undefined
NullSafe.get(obj, 'a', 'x'); // TS compilation error: Argument of type '"x"' is not assignable to parameter of type '"b" | "c" | "b"'.

NullSafe.default(obj, 'a', 'b', 456); // returns 123
NullSafe.default(obj, 'a', '5', 5, 'value'); // returns 'value'
NullSafe.default(obj, 'a' 'd', 'e', true); // returns true

NullSafe.set(obj, 'a', 'd', 'e', true); // obj.a.d is now { e: true }
NullSafe.set(obj, 'a', 'b', 'abc'); // TS compilation error: Argument of type '"abc"' is not assignable to parameter of type 'number'.

Readme

Keywords

none

Package Sidebar

Install

npm i ts-null-safe

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

8.51 kB

Total Files

5

Last publish

Collaborators

  • shalom_pele