Skip to content

Possible loss of Attachments if restProps is processed using a "for in" loop. #15929

Open
@HighFunctioningSociopathSH

Description

Describe the bug

After checking why Attachments weren't working for my components, I found out that "for in" loops don't iterate over Symbols, and if they were used to process and return a new object of props from the restProps, there is a chance that Attachments would be lost in the process. As someone who doesn't have much experience with Symbols, it bugged me a little bit, and I was hoping there could be at least a warning in the docs for this issue.

Reproduction

Let's say someone wants to filter the restProps using a "for in" loop.

<script lang="ts">
  let { children, ...props } = $props();

  function processProps(props: Record<any, any>) {
    const processedProps: Record<any, any> = {};

    for (const key in props) {
      if (key !== "test") processedProps[key] = props[key];
    }

    return processedProps;
  }

  const processedProps = $derived(processProps(props));
</script>

<button {...processedProps}>
  {@render children?.()}
</button>

In the code above, the user accidentally removed the Attachments or any other property with a key of Symbol from their processedProps.
Of course, this could be fixed if you were to initially spread the original restProps inside the processedProps like so.

const processedProps: Record<any, any> = { ...props };

But when you update your Svelte version and then your Attachments don't work, it could be hard at first to know why this is happening, especially in a large code base, since it's silent with no error as well.

Logs

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-12650H
    Memory: 6.61 GB / 15.63 GB
  Binaries:
    Node: 23.11.0 - C:\nvm4w\nodejs\node.EXE
    npm: 11.3.0 - C:\nvm4w\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (136.0.3240.64)
    Internet Explorer: 11.0.26100.1882

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