Skip to content

Svelte 5: Tell if a bindable prop is actually passed using bindΒ #11672

@jamesst20

Description

@jamesst20

Describe the problem

Svelte 5 warns when non-binded props are mutated which is a very good thing to avoid unexpected side effect.

However, there is no way to tell if a prop is actually binded or not, or at least if there is it doesn't appear to be documented.

Shallow/deep copy could be avoided in some scenarios where performance matter if it's not even used.

Describe the proposed solution

Here is a real life possible scenario where you might just want to call a callback with the new values without mutating the actual passed value.

<!-- MyComponent.svelte -->
<script>
let { values = $bindable(), onChange } = $props();

const addValue = (newValue) => {
  if (isBindedProp(values)) {
    values ||= [];
    values.push(newValue);
    onChange?.(values);
  } else {
    onChange?.([...(values || []), newValue]);
  }  
};
</script>

....

<!-- Somewhere -->
<MyComponent bind:values={...}>

<!-- Somewhere else -->
<MyComponent values={...} onChange{...}>

Importance

must have.

Svelte 4 librairies should be able to tell that as well to preserve compatibility without switching to runes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions