-
Notifications
You must be signed in to change notification settings - Fork 431
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
Support for structuredClone
#1237
Comments
Thanks for the ping. It will be automatically added when it gets multiple implementations. |
Note: declare function structuredClone(value: any, options?: StructuredSerializeOptions): any; And 4.1.8 breaks |
Yeah, #1282. 😬 |
fixed with #1283 |
I guess this one still needs to be done |
Any progress here? |
PRs welcome. |
FWIW, one issue I ran into with my own generic version is that if the source object is (deeply) In my case, the whole point was to clone the template object a few times and then write to the clones, so I had to add a hacky |
Any chance we could get a typed return value in the future? Something like I know that |
My current "workaround" (let me know what you think) type MyType = {
ok?: boolean
}
const foo: MyType = {}
const bar = structuredClone(foo) as typeof foo
/*
Why use "typeof foo" instead of "MyType" above?
- To be sure to pick the right type
- To define the type only in 1 place (at the definition of "foo")
- That way, changing the type of "foo" in the future will automatically apply to "bar".
*/
bar.ok = true // OK
bar.something = 'else' // Typescript error, because bar belongs to MyType |
This type declaration works for me: type Cloneable<T> = T extends Function | Symbol
? never
: T extends Record<any, any>
? {-readonly [k in keyof T]: Cloneable<T[k]>}
: T
declare function structuredClone<T>(value: Cloneable<T>, options?: StructuredSerializeOptions | undefined): Cloneable<T>
const value = {foo: 1} as const
structuredClone(value) // { foo: 1 } Note, foo now writable
const value = {foo: 1, fn() {}} as const
structuredClone(value) // expect type error because function can't be cloned and it will throw DOMException in runtime |
That one failed for me on types containing arrays (or maybe just tuples). I managed to get my overly complex type to work using this: type Cloneable<T> = T extends Function | Symbol
? never
: T extends readonly [infer e0]
? readonly [Cloneable<e0>]
: T extends [infer e1]
? [Cloneable<e1>]
: T extends readonly [infer e2, ...infer A2]
? readonly [Cloneable<e2>, ...Cloneable<[...A2]>]
: T extends [infer e3, ...infer A3]
? [Cloneable<e3>, ...Cloneable<[...A3]>]
: T extends readonly (infer e4)[]
? readonly Cloneable<e4>[]
: T extends (infer e5)[]
? Cloneable<e5>[]
: T extends object
? { [k in keyof T]: Cloneable<T[k]> }
: T; That one does not strip |
Props to both attempts, unfortunately both fail for me with branded types, which I understand is somewhat of a second class citizen in TS, but are used in the official nominal typing example in the playground. Note I also tried the |
I'm slightly worried that the recently merged generic type signature for |
I merged it because it's certainly better than |
Well anything is better than |
Seems a bit too late to share more robust implementation, how about snippet in uhyo/better-typescript-lib#37 (comment) ? |
New DOM api
structuredClone
should be added.MDN: https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
The text was updated successfully, but these errors were encountered: