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 21, 2023
1 parent e49cbb3 commit b47991a
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
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 b47991a

Please sign in to comment.