Skip to content

Commit

Permalink
fix: maintain the object's property descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Mar 23, 2023
1 parent 064fa41 commit 5d32fcb
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/utils/src/helpers.ts
Expand Up @@ -87,10 +87,24 @@ export function clone<T>(val: T, seen: WeakMap<any, any>): T {
// we don't need properties from prototype
const props = getOwnProperties(val)
for (const k of props) {
Object.defineProperty(out, k, {
enumerable: true,
value: clone((val as any)[k], seen),
})
const descriptor = Object.getOwnPropertyDescriptor(val, k)
if (!descriptor)
continue
const cloned = clone((val as any)[k], seen)
if ('get' in descriptor) {
Object.defineProperty(out, k, {
...descriptor,
get() {
return cloned
},
})
}
else {
Object.defineProperty(out, k, {
...descriptor,
value: cloned,
})
}
}
return out
}
Expand Down

0 comments on commit 5d32fcb

Please sign in to comment.