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

Using Object.defineProperty on a proxied / state wrapped object throws state_descriptors_fixed error #14320

Open
Leonidaz opened this issue Nov 15, 2024 · 0 comments

Comments

@Leonidaz
Copy link

Describe the bug

When trying to define a custom getter / setter on a state object via Object.defineProperty, svelte compiler throws the following error:

state_descriptors_fixed Property descriptors defined on `$state` objects must contain `value` and always be `enumerable`, `configurable` and `writable`.

E.g.

	let locals = $state({});
	let count = $state(0);         
	Object.defineProperty(locals, 'count', {
		get: function() { return count },
		set: function(v) { console.log('assigned', v); count = v;},
		enumerable: true,
		configurable: true,
	});

The only way to make it work which defeats the purpose of a custom getter / setter is to remove the getter and setter and provide the value option to the Object.defineProperty:

	let locals = $state({});
	let count = $state(0);
	Object.defineProperty(locals, 'count', {
		value: count,
		enumerable: true,
		configurable: true,
	});

Reproduction

Playground

Logs

No response

System Info

Playground version 5.2.0

Severity

annoyance

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

1 participant