Skip to content

Commit

Permalink
Fix possible issue with multi-typed options
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Jul 6, 2021
1 parent 5f537b2 commit 5a2617e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/vanilla/src/utils/deep-assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
export const deepAssign = <O>(target: O, source: any): O => {
for (const [key, value] of Object.entries(target)) {
const sourceValue = source[key];

// Use the default value if there's no value specified
target[key as keyof O] = source[key] === undefined ? target[key as keyof O] :
target[key as keyof O] = sourceValue === undefined ? target[key as keyof O] :

// Check if it's a nested object and merge if required
(typeof value === 'object' && value !== null && !Array.isArray(value)) ?
deepAssign(value as O, source[key] as Partial<O>) : source[key];
(typeof sourceValue === 'object' && typeof value === 'object' && value !== null && !Array.isArray(value)) ?
deepAssign(value as O, sourceValue as Partial<O>) : sourceValue;
}

return target;
Expand Down

0 comments on commit 5a2617e

Please sign in to comment.