Skip to content

SvelteSet reactivity breaks in reassignment #16422

@hyunbinseo

Description

@hyunbinseo

Describe the bug

Behaviors of 1 and 3 are inconsistent.


  1. Works without warning:
<script lang="ts">
  import { SvelteSet } from 'svelte/reactivity';

  let ids = new SvelteSet([1]);
</script>

{#each ids as id (id)}{id}{/each}

<!-- Pressing this will render 2 above -->
<button type="button" onclick={() => (ids = new SvelteSet([2]))}>
  Update IDs
</button>
  1. Breaks. Pressing the button does not render 2
<script lang="ts">
  import { SvelteSet } from 'svelte/reactivity';

  // `ids` is updated, but is not declared with `$state(...)`.
  let ids = new SvelteSet([1]);
  const count = $derived(ids.size); // added
</script>

{#each ids as id (id)}{id}{/each}

<button type="button" onclick={() => (ids = new SvelteSet([2]))}>
  Update IDs
</button>
  1. Works, but results in ESLint error:
<script lang="ts">
  import { SvelteSet } from 'svelte/reactivity';

  // SvelteSet is already reactive, $state wrapping is unnecessary.
  let ids = $state(new SvelteSet([1]));
  const count = $derived(ids.size); // added
</script>

{#each ids as id (id)}{id}{/each}

<button type="button" onclick={() => (ids = new SvelteSet([2]))}>
  Update IDs
</button>

Reproduction

https://svelte.dev/playground/31fb8c5a3285467ebbed95ec40ce7d30?version=5.36.6

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions