TehShrike / deepmerge

A library for deep (recursive) merging of Javascript objects
MIT License
2.75k stars 216 forks source link

An error occuren when merge axios config: config.signal.addEventListener is not a function #249

Closed ZLY201 closed 1 year ago

ZLY201 commented 2 years ago

An error occuren when merge axios config:

config.signal.addEventListener is not a function

You can reproduce it with the following code:

const controller = new AbortController();
cosnt config = deepmerge({}, { signal: controller.signal });
console.log(typeof config.signal.addEventListener);            // undefined
console.log(typeof controller.signal.addEventListener);        // function

Is this a bug?

RebeccaStevens commented 2 years ago

This library can't merge non-basic objects out of the box. See https://github.com/TehShrike/deepmerge#ismergeableobject

ZLY201 commented 2 years ago

I found this in the source code:

function getKeys(target) {
    return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
}

and something will be wrong with Object.keys

Object.keys(controller.signal)      // []

but for...in works fine

for (const key in controller.signal) {
    console.log(key);
}
/*
aborted
reason
onabort
throwIfAborted
addEventListener
dispatchEvent
removeEventListener
*/

May be should change to use for...in?

RebeccaStevens commented 2 years ago

There are a few reason why for..in is a bad approach so I don't see that changing.

If you want a more modern version of this library, check out https://github.com/RebeccaStevens/deepmerge-ts