Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add structuredClone support #224

Open
WinterSmileSB101 opened this issue Jul 12, 2023 · 3 comments
Open

Add structuredClone support #224

WinterSmileSB101 opened this issue Jul 12, 2023 · 3 comments

Comments

@WinterSmileSB101
Copy link

WinterSmileSB101 commented Jul 12, 2023

Decimal.js seems to not support structuredClone, this method will throw DataCloneError because of decimal instance contains a function(constructor ) due to this line code:
https://github.com/MikeMcl/decimal.js/blob/master/decimal.js#L4294

xxxx
// Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor
// which points to Object.
x.constructor = Decimal;
xxxx

Any plans to support it?

@WinterSmileSB101 WinterSmileSB101 changed the title add structuredClone support Add structuredClone support Jul 12, 2023
@MikeMcl
Copy link
Owner

MikeMcl commented Jul 12, 2023

No, no plans, and that line is essential.

Would I like to support structuredClone? Yes.
Am I going to investigate this matter right now or in the near future? Probably not.

@WinterSmileSB101
Copy link
Author

OK, thanks.

@ctsstc
Copy link

ctsstc commented Sep 9, 2023

Just landed here as structured clone is finally becoming the goto for deep cloning an object.

@WinterSmileSB101 what did you end up doing to get around this?

For now here's my painful "fix" that conforms to typing.

Here's a simple example without crazy nesting involved:

const { decimalOne, decimalTwo, ...withoutDecimals } = originalObject // originalObject satisfies SomeType
const newObjectWithoutDecimals = structuredClone(withoutDecimals)
const newObject: SomeType = {
  ...newObjectWithoutDecimals,
  decimalOne: new Decimal(decimalOne),
  decimaltwo: new Decimal(decimalTwo),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants