Skip to content

Reassignment through object spread inside a loop throwing “Invalid array length” #4574

Closed
@sunyatasattva

Description

@sunyatasattva

When I try to reassign an object via spread operator inside a loop, it throws an error. However, this works as expected outside loops.

Example

<script>
  let obj = {
    prop: "foo"
  };
</script>

<span class="content">{obj.prop}</span>
<button on:click={ () => obj = { ...obj, prop: "bar" } }>Test</button>

The above works correctly. However, the same logic inside a loop:

<script>
  let obj = {
    prop: "foo"
  };

  let arr = [obj]
</script>

{#each arr as o}
  <span class="content">{o.prop}</span>
  <button on:click={ () => o = { ...o, prop: "bar" } }>Test</button>
{/each}

This throws an Invalid array length error.

Stack trace

Object.update [as p]
https://k01vy.csb.app/App.svelte:233:28
update
https://codesandbox.io/static/js/sandbox.dc9db2542.js:1:179359
flush
https://codesandbox.io/static/js/sandbox.dc9db2542.js:1:179359

Reproduction case

Check this code sandbox: https://codesandbox.io/s/jovial-kapitsa-k01vy

Note that even inside the loop, reassignment through o = o still works, it's only when the spread operator is used that an error is thrown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions