defaults-deep-ts

_.defaultsDeep with proper typescript typing

Usage no npm install needed!

<script type="module">
  import defaultsDeepTs from 'https://cdn.skypack.dev/defaults-deep-ts';
</script>

README

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;