Skip to content

feat: make deriveds writable #15570

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

Merged
merged 8 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-terms-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: make deriveds writable
4 changes: 4 additions & 0 deletions documentation/docs/02-runes/03-$derived.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Anything read synchronously inside the `$derived` expression (or `$derived.by` f

To exempt a piece of state from being treated as a dependency, use [`untrack`](svelte#untrack).

## Overriding derived values

Derived expressions are recalculated when their dependencies change, but you can temporarily override their values by reassigning them. This can be useful for things like _optimistic UI_, where a value is derived from the 'source of truth' (such as data from your server) but you'd like to show immediate feedback to the user.

## Update propagation

Svelte uses something called _push-pull reactivity_ — when state is updated, everything that depends on the state (whether directly or indirectly) is immediately notified of the change (the 'push'), but derived values are not re-evaluated until they are actually read (the 'pull').
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export function validate_assignment(node, argument, state) {
const binding = state.scope.get(argument.name);

if (state.analysis.runes) {
if (binding?.kind === 'derived') {
e.constant_assignment(node, 'derived state');
}

if (binding?.node === state.analysis.props_id) {
e.constant_assignment(node, '$props.id()');
}
Expand All @@ -38,25 +34,6 @@ export function validate_assignment(node, argument, state) {
e.snippet_parameter_assignment(node);
}
}
if (
argument.type === 'MemberExpression' &&
argument.object.type === 'ThisExpression' &&
(((argument.property.type === 'PrivateIdentifier' || argument.property.type === 'Identifier') &&
state.derived_state.some(
(derived) =>
derived.name === /** @type {PrivateIdentifier | Identifier} */ (argument.property).name &&
derived.private === (argument.property.type === 'PrivateIdentifier')
)) ||
(argument.property.type === 'Literal' &&
argument.property.value &&
typeof argument.property.value === 'string' &&
state.derived_state.some(
(derived) =>
derived.name === /** @type {Literal} */ (argument.property).value && !derived.private
)))
) {
e.constant_assignment(node, 'derived state');
}
}

/**
Expand All @@ -81,7 +58,6 @@ export function validate_no_const_assignment(node, argument, scope, is_binding)
} else if (argument.type === 'Identifier') {
const binding = scope.get(argument.name);
if (
binding?.kind === 'derived' ||
binding?.declaration_kind === 'import' ||
(binding?.declaration_kind === 'const' && binding.kind !== 'each')
) {
Expand All @@ -96,12 +72,7 @@ export function validate_no_const_assignment(node, argument, scope, is_binding)
// );

// TODO have a more specific error message for assignments to things like `{:then foo}`
const thing =
binding.declaration_kind === 'import'
? 'import'
: binding.kind === 'derived'
? 'derived state'
: 'constant';
const thing = binding.declaration_kind === 'import' ? 'import' : 'constant';

if (is_binding) {
e.constant_binding(node, thing);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: `
<input type="range"><input type="range"><p>0 * 2 = 0</p>
`,

ssrHtml: `
<input type="range" value="0"><input type="range" value="0"><p>0 * 2 = 0</p>
`,

test({ assert, target, window }) {
const [input1, input2] = target.querySelectorAll('input');

flushSync(() => {
input1.value = '10';
input1.dispatchEvent(new window.Event('input', { bubbles: true }));
});

assert.htmlEqual(
target.innerHTML,
`<input type="range"><input type="range"><p>10 * 2 = 20</p>`
);

flushSync(() => {
input2.value = '99';
input2.dispatchEvent(new window.Event('input', { bubbles: true }));
});

assert.htmlEqual(
target.innerHTML,
`<input type="range"><input type="range"><p>10 * 2 = 99</p>`
);

flushSync(() => {
input1.value = '20';
input1.dispatchEvent(new window.Event('input', { bubbles: true }));
});

assert.htmlEqual(
target.innerHTML,
`<input type="range"><input type="range"><p>20 * 2 = 40</p>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let count = $state(0);
let double = $derived(count * 2);
</script>

<input type="range" bind:value={count} />
<input type="range" bind:value={double} />

<p>{count} * 2 = {double}</p>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.