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

feat: allow modifiying derived props #10080

Merged
merged 2 commits into from
Jan 9, 2024
Merged

Conversation

dummdidumm
Copy link
Member

Started this when looking into #9998 and thinking $derived is necessary - turns out it's not, but I thought I'd create this PR anyway and put it up for discussion whether or not this should be allowed.

Pros:

  • You can write code that's valid using it, so it may be overzealous to disallow it
  • You can hide the $derived behind a getter and then the validation wouldn't catch it anyway
  • We do allow it for the bind:x case

Cons:

  • May allow spaghetti code

Personally I lean towards relaxing this rule.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jan 4, 2024

🦋 Changeset detected

Latest commit: 1794b0f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-5-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 4, 2024 0:54am

@Prinzhorn

This comment was marked as outdated.

@brunnerh
Copy link
Member

brunnerh commented Jan 4, 2024

I have also come across this with e.g. editing a selected object from a list via some key. E.g.

const items = $state([
	{ text: 'Item 1' },
	{ text: 'Item 2' },
	{ text: 'Item 3' },
]);
let index = $state(0);
const current = $derived(items[index]);

const onClearCurrent = () => {
	current.text = ''; // error
};

Would be in favor of allowing this.
Related:

@Prinzhorn
In Svelte 5 reassigning a derived variable is a compiler error. So the only way to create an inconsistency would be to create a new object in $derived and modifying a property; if the returned object is an existing one, the $derived's logic should still hold.

But as dummdidumm pointed out, creating this inconsistency is already possible in various ways, including bindings and some indirection.

@Prinzhorn
Copy link
Contributor

In Svelte 5 reassigning a derived variable is a compiler error

I assumed this PR was about removing this restriction. If it's only about mutating derived objects then ignore my comment above.

@dummdidumm dummdidumm merged commit 3624a4c into main Jan 9, 2024
9 checks passed
@dummdidumm dummdidumm deleted the allow-mutating-derived-props branch January 9, 2024 09:29
trueadm pushed a commit that referenced this pull request Jan 9, 2024
It's an unnecessary restruction because it can be worked around (hide it behind a getter/setter), already works for bind:x and prevents valid use cases
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

Successfully merging this pull request may close these issues.

None yet

3 participants