Skip to content

Allow creating read-only accessors of primitive states #10310

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

Closed
Thiagolino8 opened this issue Jan 26, 2024 · 6 comments · Fixed by #10430
Closed

Allow creating read-only accessors of primitive states #10310

Thiagolino8 opened this issue Jan 26, 2024 · 6 comments · Fixed by #10430
Milestone

Comments

@Thiagolino8
Copy link

Thiagolino8 commented Jan 26, 2024

Describe the problem

In Svelte 3/4 the export keyword had two different meanings within components, create props and create accessors

In Svelte 5 the keyword has only 1 meaning, creating read-only accessors, and if you want to add the ability to write to the accessor the value also needs to be declared as a prop

However, it is not possible to create let variable accessors this way

export let count = $state({value: 0})

only this way

let count = $state({value: 0})
export {count}

And it is also not possible to create state accessors for let variables that are reassigned without also declaring it as a prop

Describe the proposed solution

Allow the use of export let in runes mode for the purpose of creating read-only accessors and allow the creation of read-only accessors for primitive states

Importance

would make my life easier

@dummdidumm
Copy link
Member

The comparison isn't fully correct. In Svelte 4, export let created a property, and it was only available as an accessor if you turned on the corresponding compiler option. In this regard nothing has changed, you still have the accessor option available to you in Svelte 5.

Can you give an example what exactly is no longer achievable in Svelte 5? What's the use case behind this?

@dummdidumm dummdidumm added the awaiting submitter needs a reproduction, or clarification label Feb 6, 2024
@dummdidumm dummdidumm added this to the 5.0 milestone Feb 6, 2024
@Thiagolino8
Copy link
Author

It is not an existing feature in V4
I don't know if it was on purpose, but it is now possible to create let variable accessors without turning them into props and without using the accessors option, unlike V4.
The accessors option creates an accessor with getter and setter for all props, my suggestion is to allow creating an accessor with just the getter, without transforming it into a prop using export let and for individual states instead of all props

@dummdidumm
Copy link
Member

It would be confusing to explain why you can export $state like that from a component and it behaves differently compared to an export from a module. The "workaround" is to just export a function which returns the current value, which also better conveis the "this can change" notion -> this code should be a compiler error.

@dummdidumm dummdidumm removed the awaiting submitter needs a reproduction, or clarification label Feb 8, 2024
dummdidumm added a commit that referenced this issue Feb 8, 2024
disallow exporting props, derived and reassigned state from within components
closes #10310, closes #10311, closes #10293
dummdidumm added a commit that referenced this issue Feb 8, 2024
disallow exporting props, derived and reassigned state from within components
closes #10310, closes #10311, closes #10293
@Thiagolino8
Copy link
Author

Ok, I understand better what the intention is

It seems strange to me to keep the same restrictions inside and outside a component, even if the export keyword being used is the same it is doing different things

But if what is being sought is syntax parity with modules then it should be possible to use export let with declarations that are not reassigned

@dummdidumm
Copy link
Member

In Svelte 7 that will probably be allowed but until then export let means "a property export" and it's better to throw a helpful error ("not allowed, you need to use $props()") which catches the majority case (people wanting to declare props) compared to this very minor case.

@Rich-Harris
Copy link
Member

note that we reverted this change in #10523, this will now work

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 a pull request may close this issue.

3 participants