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

$props & $derived should use let instead of const #12121

Closed
bfanger opened this issue Jun 21, 2024 · 4 comments
Closed

$props & $derived should use let instead of const #12121

bfanger opened this issue Jun 21, 2024 · 4 comments

Comments

@bfanger
Copy link
Contributor

bfanger commented Jun 21, 2024

Describe the bug

At the moment (Svelte 5.0.0-next.162) both:

const { value } = $props()

and

let { value } = $props()

work without issue or warning.

Allowing const here is confusing as the meaning in Javascript is that the value is never re-assigned, but in case of $props and $derived the value will be reassigned. It's not something we do, but something the Svelte reactivity system does.

Although I think let is the correct declaration type, const has the benefit of blocking assignments Cannot assign to constant and we shouldn't reassign most props or derived values.

For derived values the compiler already protects us against that Cannot assign to derived state 🎉
( It would be nice if the compiler also protects us against assigning against assigning values to nonbindable props.)

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE42QwWrDMAyGX0WIHhIa6uWaOYGxvcW8Q-Y61MyRjS0PSsi7DzdtYLDDDjro1_dLP1pwss4k7N4XpHE22OFLCNggX0Np0rdxbLDB5HPURZFJRxt4UKTYzsFHhjebghuvMEU_g8KTuAunza2wsKWcYdA-E0MPh8Qjm-qpflZUZlMmzdYTWNLRzIa4qmG5OXnzHHtoC8yrIin2GPJxfrlhK4gifmZmT-BJO6u_-mXfug7HVoptPGCDsz_byZozdhyzWZv9D_e1__2F9pR4z9DDIUQfUlX_yqpIXtrhtTDdg5Xi0v4R5GP9AYfFjG-cAQAA

Logs

No response

System Info

System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 112.45 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    Yarn: 1.22.21 - ~/Library/pnpm/yarn
    npm: 10.8.1 - /opt/homebrew/bin/npm
    pnpm: 9.4.0 - ~/Library/pnpm/pnpm
  Browsers:
    Brave Browser: 125.1.66.110
    Chrome: 126.0.6478.63
    Safari: 17.5
  npmPackages:
    svelte: 5.0.0-next.162 => 5.0.0-next.162

Severity

annoyance

@dummdidumm
Copy link
Member

This comes down to stylistic preference. I personally for example want to use const for components that only have readonly props. Or I prefer to use const for $derived because for me it better communicates that this is a readonly value, not necessarily that the value itself never changes.

We shouldn't be in the business of dictating these preferences, therefore closing.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2024
@bfanger
Copy link
Contributor Author

bfanger commented Jun 21, 2024

Javascript already has a muddy definition of const but the following should impossible in javascript with const

const { value } = $props()

console.log(value) // 1

function doSomethingLater{
    console.log(value) // 4
}

The const declaration creates an immutable reference to a value.
The variable identifier cannot be reassigned.
You should understand const declarations as "create a variable whose identity remains constant"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

Silently converting the behavior of const to let for signals doesn't feel like a stylistic preference to me.

@dummdidumm
Copy link
Member

You think about it that way, I think about it completely different. For me it's that I as the user am not allowed to reassign the derived value / the prop, not that the value doesn't change.
So yes this is absolutely stylistic preferences.

@bfanger
Copy link
Contributor Author

bfanger commented Jun 26, 2024

I've written a ESLint rule svelte/rune-prefer-let to automatically convert const to let for reactive variables:
sveltejs/eslint-plugin-svelte#806

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