defaults-deep-ts
v0.2.3
Published
_.defaultsDeep with proper typescript typing
Downloads
1,043
Maintainers
Readme
defaults-deep-ts
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;