svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Add setter to let variables accessors

Open Thiagolino8 opened this issue 1 year ago • 3 comments

Describe the problem

In Svelte 3/4 the export keyword created accessors for const variables and functions and created props for let variables and when using the accessors option the keyword also created accessors for let variables In Svelte 5, the export keyword only creates accessors, but unlike v4 accessors, v5 accessors only have read capacity, creating a feature disparity between versions

Describe the proposed solution

Add setters to accessors of variables declared using let exported from a component

Importance

would make my life easier

Thiagolino8 avatar Jun 10 '24 03:06 Thiagolino8

Exporting a let variable in v4 is a property. The equivalent in v5 should not be an export but a $props() declaration.

(There also is a compiler option legacy.componentsApi which might have an effect on the behavior - see these docs. At the very least you would be able to use $set on the component.)

brunnerh avatar Jun 10 '24 08:06 brunnerh

You still can export function-setters

export const setMyProp = (v) => { myProp = v; };

or an object with all [writable] props

export const options1 = someStateObject;
export const options2 = {
  get myProp() { return myProp; },
  set myProp(v) { myProp = v; },
};

7nik avatar Jun 10 '24 09:06 7nik

This was a deliberate choice though I can't exactly remember the rationale. Adding the 5.0 milestone so that we reach a final decision on this before the semver window closes

Rich-Harris avatar Jun 10 '24 16:06 Rich-Harris

This was originally requested in #10310 (the readonly version) and then implemented in #10523 but without any real rationale for one way or the other. The only one I could imagine is "it makes the difference between export { x } in regular modules and component instances even more drastic".

I would be ok with adjusting that so that $state exports are settable.

dummdidumm avatar Jul 08 '24 11:07 dummdidumm