structuredClone not keeping Date type

290 views Asked by At

I'm using the structuredClone() global function in NodeJs 20.4.1 to clone an object that has a property of type Date. However, the clone object is not keeping the type of this object.

const originalObj = { myDate: new Date("2023-01-01 00:00:00") };
const cloneObj = structuredClone(originalObj);

console.log({ originalDate: originalObj.myDate, instanceofDate: originalObj.myDate instanceof Date });
console.log({ cloneDate: cloneObj.myDate, instanceofDate: cloneObj.myDate instanceof Date });

The output is:

{ originalDate: 2023-01-01T00:00:00.000Z, instanceofDate: true }
{ cloneDate: 2023-01-01T00:00:00.000Z, instanceofDate: false }

Am I doing something wrong? Or there is something wrong with structuredClone()?

0

There are 0 answers