Skip to content

Allow assigning to properties of $derived #15227

@PuruVJ

Description

@PuruVJ

Describe the bug

For this code:

class Linked<T> {
  #source = $state() as () => T;
  #derived = $derived.by(() => {
    let val = $state(this.#source());

    return {
      get current() {
        return val;
      },

      set current(b) {
        val = b;
      },
    };
  });

  constructor(source: () => T) {
    this.#source = source;
  }

  get current() {
    return this.#derived.current;
  }

  set current(value: T) {
    this.#derived.current = value;
  }
}

The line this.#derived.current = value errors saying cannot assign to derived, whereas this works completely fine in a regular component, outside of a class.

Reproduction

https://svelte.dev/playground/e8bd341604ce403fa3e65c3c33a85193?version=5.17.3#H4sIAAAAAAAAE3VSwW6jMBD9ldF0DyCh5E4Aqd3rHntb78GA26CSsWUPbCvEv69sQyBN9oA8en5v_OYxE5K8KMzxRX9ihm9drxzmvyfkL-NhD2C2kp6NObhR9eyxWjr1CG80sSJ2mGPhGtsZrgQJ7i5GW4af-mI0KWJ4s_oCAg_HK7T0EHjyAv_1isFYbaCEH44lq0SgwPQkqDhuvclIq4hzKDoyA0PdUZuPsh9UOXn1DMdKUCwFFWdbCSo2IzuOIMyQ1SdjznZQc_afIL5bvo3jwe19KNBLei8FshNYrbNOcdrZz-srl6Qhi6aXzsGvjj5UW7xWMAkCeHJ6sI3asklBOkhSKCt4PQVGq2w3qtZTlvJQfyWBMoWMw6uj7LcmfO7cYWmdpOH5QLSKB0uwyAS_K4ZmsD74JN3gK3GU_WkF52ztItjtdPWNMNqor6JYhGOOPgAaTY7t0LC2SfSYrxOnMRWA_QRQQixCHnNscmfdqxbbUbyGtbD24r3_sGP58rQ3-1ANJQTi0mSOWYRJ4gWUQOrv8nuTJC0r_-_vtrw5d337cMnDsT7nNxm-Qbdb_Wf-B7uk2Fz3AwAA

Logs

System Info

System:
    OS: macOS 15.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 127.20 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.3.0 - ~/.volta/tools/image/node/22.3.0/bin/node
    Yarn: 4.3.1 - ~/.volta/tools/image/yarn/4.3.1/bin/yarn
    npm: 10.8.1 - ~/.volta/tools/image/node/22.3.0/bin/npm
    pnpm: 9.14.2 - ~/.volta/bin/pnpm
    bun: 1.1.38 - ~/.bun/bin/bun
    Watchman: 2024.11.18.00 - /opt/homebrew/bin/watchman
  Browsers:
    Edge: 132.0.2957.140
    Safari: 18.3

Severity

annoyance

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions