Skip to content

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

@Leonidaz

Description

@Leonidaz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions