-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Svelte 5: structuredClone tries to clone proxy object instead of its contents #13562
Comments
This is what https://svelte-5-preview.vercel.app/docs/runes#$state-snapshot is for. I don't know whether it makes sense to patch |
I'm aware of that, but there is zero indication of a problem in code editor. Svelte lies to TypeScript that I don't know what will Svelte team do regarding this issue, but this definitely should be fixed one way or another. I would suggest a fix but I am by no means qualified to talk about tool design (especially given the scale of Svelte). |
I would really love the addition of |
We can probably monkey patch |
Just one more thing I wanna say here, If a function returns an object that is something like:
Just by seeing the type, you have no idea if this is reactive or not, You have no idea that if you do, |
Not much we can do about this with today's tooling. The same issue applies with any proxy reactivity library. |
If the return type of signals were |
@FoHoOV No, that wouldn't help at all. Boxing things doesn't solve anything – it just moves the problem to another area and in this case doesn't solve the problem of proxies. Unless you expected each property of the object/array to also be boxed – but that's terrible ergonomics. |
I must be using the wrong terms here, |
You would need to add a property or symbol to the type, otherwise the wrapper type gets erased. And if you do that, you are no longer allowed to assign to the variable, because the added property will be missing from the new value. const stateKey = Symbol('state-key');
type StateProxy<T> = T & { [stateKey]: unknown };
declare function $state(value: number): number;
declare function $state(value: string): string;
// ...
declare function $state<T>(value: T): StateProxy<T>;
let a = $state(5); // number
let b = $state({ value: 3 }); // StateProxy<{ value: number }>
b = { value: 5 }; // Error |
You need to make the symbol optional let a = $state(5); // StateProxy<number & { [stateKey]?: unknown }>
let b = a; // b will have the same reactive type while, in fact, it is static |
That is fine, it's a statement about the object, not the variable. |
It should be recursive (it's possible) but only for POJOs. In TS, a class instance and a POJO with the same fields are the same thing, aren't they? |
Their prototypes also have to be the same. |
A mean this case which results in a wrong type |
Describe the bug
Attempting to use
structuredClone
on any stateful object will result in an error with zero indication of a problem before running the code in both JavaScript and TypeScript.Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACm2QwUrEQAyGXyVEobtQ2nu3LojiwYvHPTgeujPpOjqblJmMq5S-uwwLHoq35OP7_0BmHH2ghN3rjDycCTu8nyasUX-msqQvCkpYY5IcbSF9stFPujdsNJBCThThDm6TDkqbuWCjpaoDg8_yzvAoZLDwZbszXAYrnCRQE-S0KfntDtoWDhI_E4ye6T-nKZ1rEYYEFwphHUgas9UcyT0EYboeuYafBh_IgQrQN9msBNXKrkAYqoNnJ5eqg5v-5fhBVvdgJQcHLApHAltU1xju27-PYI1ncX705LDTmGl5W34BHhXdL2IBAAA=
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: