Skip to content

Cannot spread class instances as props #16307

Closed as not planned
Closed as not planned
@sacrosanctic

Description

@sacrosanctic

Describe the bug

Since Svelte pushes for the use of JS classes, it seems like I would also want to be able to spread them as I do with objects. Currently, it just doesn't work. There are workarounds, such as arrow functions or .bind(this) (examples in the REPL). Would like clarification on whether any of these methods are recommended.

<script>
  import Child from './Child.svelte'

  class Counter {
    count = $state(0)

    constructor(input) {
      this.count = input
    }

    onAdd() {
      this.count++
    }
  }

  const counter2 = new Counter(0)
</script>

<h2>❌ spread props</h2>
<Child {...counter2} />
<hr />

Other

Discord Thread

Reproduction

REPL

Logs

System Info

.

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