import { deepClone } from './deepClone' ; import { isObject } from './isObject' ; import type { DeepRequired , NonNullObject } from './utilityTypes' ; remark remark remark param base param overwrites example example example example example export function mergeDefault < A extends NonNullObject , B extends Partial < A > > ( base , overwrites ? ) { if ( ! overwrites ) return deepClone ( base ) as DeepRequired < A & B > ; for ( const [ baseKey , baseValue ] of Object . entries ( base ) ) { const overwritesValueAtBaseKey = Reflect . get ( overwrites , baseKey ) ; if ( typeof overwritesValueAtBaseKey === 'undefined' ) { Reflect . set ( overwrites , baseKey , deepClone ( baseValue ) ) ; } else if ( isObject ( overwritesValueAtBaseKey ) ) { Reflect . set ( overwrites , baseKey , mergeDefault ( ( baseValue ?? { } ) as NonNullObject , overwritesValueAtBaseKey ) ) ; } } return overwrites as DeepRequired < A & B > ; }