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 StructuredCloneable type #897

Merged
merged 4 commits into from
Jun 26, 2024

Conversation

thecodewarrior
Copy link
Contributor

This adds two types for values losslessly cloneable using structuredClone.

Notes:

  • The web and non-web types are separate so people who aren't writing browser code don't pull in web types
  • Custom error classes do match, despite the fact that structuredClone isn't able to losslessly clone them (they're cloned as base Error instances). After doing some research I couldn't figure out a way to include the root Error class but exclude subclasses, so I just accepted the limitation and documented the discrepancy.

Fixes #855

@Emiyaaaaa
Copy link
Collaborator

I think this PR needs to wait for #898 to merge.

@sindresorhus
Copy link
Owner

Tests are failing now. Not sure how we can handle DOM types conditionally.

@thecodewarrior
Copy link
Contributor Author

I'm not sure there's a way to conditionally enable dom types. I tried using a triple-slash directive but that seems to include the DOM types across the entire project.

Though if we aren't including DOM types in type-fest in general, we could just remove the web variant. The only DOM types I can really foresee having widespread use would be Blob and File, both of which seem to compile successfully without the DOM library, so they can just be folded into the standard variant.

Removed the dedicated web variant to remove the dependency on the DOM library. `Blob` and `File` compile without DOM and were folded into the standard variant.
@sindresorhus
Copy link
Owner

Relevant: microsoft/TypeScript#31894

@sindresorhus
Copy link
Owner

I think we could do it by adding a dom sub-export in package.json: https://nodejs.org/api/packages.html#subpath-exports

@thecodewarrior thecodewarrior changed the title Add StructuredCloneable/StructuredCloneableWeb types Add StructuredCloneable type Jun 25, 2024
@thecodewarrior
Copy link
Contributor Author

Adding the sub-export would probably involve changes to the testing/build infrastructure (running twice for non-dom/dom builds), which I'm not familiar with, but if you want to implement that I'd be happy to add back the web type. If we're doing a sub-export I think it wouldn't even need to have a separate name, just import {StructuredCloneable} from 'type-fest/dom' instead of import {StructuredCloneable} from 'type-fest'

@sindresorhus sindresorhus merged commit 737550b into sindresorhus:main Jun 26, 2024
8 checks passed
@sindresorhus
Copy link
Owner

Thanks :)

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

Successfully merging this pull request may close these issues.

Type for serializable objects?
3 participants