1.0.0 • Published 2 years ago

@paescuj/defaults-deep-ts v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

defaults-deep-ts

Build Status

Similar to _.defaultsDeep but with some differences:

  • Intelligent TypeScript typings
  • Doesn't try to merge arrays
  • Doesn't allow multiple sources
  • Treats null as an empty value

Install

npm install defaults-deep-ts

Usage

// let's say you have some object where some fields are optional:
type SomeData = {
  order?: {
    id?: string
  };
}
const data: SomeData = {};

// You have to check for for undefined fields using `!` or `?`:
const id: string = data.order!.id!;

// Let's provide some defaults:
import defaultsDeep from 'defaults-deep-ts'
const dataWithDefaults = defaultsDeep(data, {order: {id: ''}});

// Now `order` and `id` are not optional bacause you provided defaults for them:
const id: string = dataWithDefaults.order.id;