Skip to content

Svelte 5: Snippets available in <script> #2201

@eddiemcconkie

Description

@eddiemcconkie

Describe the bug

I just found out that if you create a {#snippet} in your component, you can reference it from the script tag:

<script>
  console.log(mySnippet) // Shows type error, but works fine
</script>

{#snippet mySnippet()}
  ...
{/snippet}

Not sure if this is a bug, or if this is actually ok behavior and the type checker just marks it as an error since the snippet function isn't declared inside the script tag. I do see this as potentially useful, though, if you wanted to pass a snippet around through a store or context (see REPL). And in that case, the fix could probably just be to make intellisense aware of snippets inside the script tag.

Reproduction

REPL

This actually looks and works fine in the REPL. If you have this same code in VS Code though, it will highlight mySnippet as an error inside setContext

Logs

No response

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 460.05 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
    pnpm: 8.6.3 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 119.0.6045.123
    Safari: 17.0
  npmPackages:
    svelte: 5.0.0-next.1 => 5.0.0-next.1

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions