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

Svelte 5: False positive ownership_invalid_mutation on bind used with state declared in .svelte.ts #12716

Open
someone2080 opened this issue Aug 3, 2024 · 4 comments

Comments

@someone2080
Copy link

Describe the bug

Can't bind state declared in classes like this:

// MyController.svelte.ts
class MyControllerClass {
    form_state = $state<{fullname: string}>({fullname: ''});
}

Svelte throws ownership_invalid_mutation warning.

It seems to be related to this issue.
No warning is shown if I use a function instead of a class:

// MyController.svelte.ts
function MyControllerClass() {
 	let form_state = $state<{fullname: string}>({fullname: ''});

 	return {
 		form_state
 	}
 }

Reproduction

Full reproduction can be found here: REPL

Logs

No response

System Info

"svelte": "^5.0.0-next.208",

Severity

annoyance

@paoloricciuti
Copy link
Member

I can't seem to get a warning in the repl. Can you help me reproduce?

@someone2080
Copy link
Author

Sorry, my bad.

REPL doesn't show this warning. You'll have to reproduce it on your local.

This is what I get in my console:

Child.svelte:8 [svelte] ownership_invalid_mutation
src/Child.svelte mutated a value owned by src/App.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead

This error doesn't show up if I use a function instead of a class.

REPL should have everything included to reproduce it on your local.

Please let me know if you need anything else, or if I'm doing something wrong.

package.json

{
  "name": "playground",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "check": "svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"
  },
  "devDependencies": {
    "@sveltejs/vite-plugin-svelte": "^3.1.1",
    "@tsconfig/svelte": "^5.0.4",
    "svelte": "^5.0.0-next.208",
    "svelte-check": "^3.8.4",
    "tslib": "^2.6.3",
    "typescript": "^5.2.2",
    "vite": "^5.3.4"
  }
}

Thanks!

@paoloricciuti
Copy link
Member

Sorry, my bad.

REPL doesn't show this warning. You'll have to reproduce it on your local.

This is what I get in my console:

Child.svelte:8 [svelte] ownership_invalid_mutation
src/Child.svelte mutated a value owned by src/App.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead

This error doesn't show up if I use a function instead of a class.

REPL should have everything included to reproduce it on your local.

Please let me know if you need anything else, or if I'm doing something wrong.

package.json

{
  "name": "playground",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "check": "svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"
  },
  "devDependencies": {
    "@sveltejs/vite-plugin-svelte": "^3.1.1",
    "@tsconfig/svelte": "^5.0.4",
    "svelte": "^5.0.0-next.208",
    "svelte-check": "^3.8.4",
    "tslib": "^2.6.3",
    "typescript": "^5.2.2",
    "vite": "^5.3.4"
  }
}

Thanks!

MMM it's weird that it doesn't reproduce in repl, the related issue talks about hmr...maybe it only happens if you modify one of the files to cause hmr?

@someone2080
Copy link
Author

Sorry, my bad.
REPL doesn't show this warning. You'll have to reproduce it on your local.
This is what I get in my console:

Child.svelte:8 [svelte] ownership_invalid_mutation
src/Child.svelte mutated a value owned by src/App.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead

This error doesn't show up if I use a function instead of a class.
REPL should have everything included to reproduce it on your local.
Please let me know if you need anything else, or if I'm doing something wrong.
package.json

{
  "name": "playground",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "check": "svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"
  },
  "devDependencies": {
    "@sveltejs/vite-plugin-svelte": "^3.1.1",
    "@tsconfig/svelte": "^5.0.4",
    "svelte": "^5.0.0-next.208",
    "svelte-check": "^3.8.4",
    "tslib": "^2.6.3",
    "typescript": "^5.2.2",
    "vite": "^5.3.4"
  }
}

Thanks!

MMM it's weird that it doesn't reproduce in repl, the related issue talks about hmr...maybe it only happens if you modify one of the files to cause hmr?

This one might be a bit different since I get the same warning with hmr disabled.

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

2 participants