{"record":{"id":"c0f4b9a1dd4e4d98","repo":"sveltejs/svelte","slug":"state-prototype-fixed","errorCode":"state_prototype_fixed","errorMessage":"state_prototype_fixed\nCannot set prototype of `$state` object\nhttps://svelte.dev/e/state_prototype_fixed","messagePattern":"state_prototype_fixed\nCannot set prototype of `\\$state` object\nhttps://svelte\\.dev/e/state_prototype_fixed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":476,"sourceCode":"\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_descriptors_fixed`);\n\t}\n}\n\n/**\n * Cannot set prototype of `$state` object\n * @returns {never}\n */\nexport function state_prototype_fixed() {\n\tif (DEV) {\n\t\tconst error = new Error(`state_prototype_fixed\\nCannot set prototype of \\`$state\\` object\\nhttps://svelte.dev/e/state_prototype_fixed`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_prototype_fixed`);\n\t}\n}\n\n/**\n * Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`\n * @returns {never}\n */\nexport function state_unsafe_mutation() {\n\tif (DEV) {\n\t\tconst error = new Error(`state_unsafe_mutation\\nUpdating state inside \\`$derived(...)\\`, \\`$inspect(...)\\` or a template expression is forbidden. If the value should not be reactive, declare it without \\`$state\\`\\nhttps://svelte.dev/e/state_unsafe_mutation`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_unsafe_mutation`);\n\t}\n}","sourceCodeStart":458,"sourceCodeEnd":494,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L458-L494","documentation":"Thrown at internal/client/proxy.js:351 in the Proxy setPrototypeOf trap of a $state object. Svelte's reactivity and forking rely on the state object's prototype staying fixed; changing the prototype via Object.setPrototypeOf would invalidate the internal signal bookkeeping and Proxy invariants, so it is unconditionally blocked.","triggerScenarios":"Calling Object.setPrototypeOf(stateObj, newProto) where stateObj is a $state-proxied object (e.g. `$state({})` or `$state(array)`). The trap at proxy.js:350-352 always throws.","commonSituations":"Assigning a class prototype onto a plain state object to add methods; mixing prototype-based inheritance with runes state; libraries that call setPrototypeOf for mixin patterns; attempted prototype pollution.","solutions":["Create the object with the desired prototype first, then wrap it in $state: `const obj = Object.create(proto); const state = $state(obj)`.","Store methods separately (e.g. on a class instance kept in $state) rather than reassigning prototypes.","Use composition: keep data in $state and behaviour in plain functions that take the state as an argument."],"exampleFix":"// before\nconst state = $state({});\nObject.setPrototypeOf(state, methodsProto); // throws\n\n// after\nconst obj = Object.create(methodsProto);\nconst state = $state(obj);","handlingStrategy":"validation","validationCode":"// Never call Object.setPrototypeOf on a $state object.\n// Create the object with the prototype first, then wrap in $state:\n// const obj = Object.create(proto); const state = $state(obj);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not call Object.setPrototypeOf on $state-proxied objects.","Set the prototype before passing the object to $state.","Use composition (separate behaviour functions) instead of prototype mutation."],"tags":["svelte","runes","state","proxy","prototype"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}