Skip to content

Svelte TypeScript declares runes in global scope, giving illusion that it is available outside of svelte files #16155

Open
@KieranP

Description

@KieranP

Describe the bug

Created from zed-extensions/svelte#34

Runes are being declared in TypeScripts global scope, leading editors to show the runes as usable in non svelte files.

declare function $state<T>(initial: T): T;
declare function $state<T>(): T | undefined;

Can the types be adjusted so that TypeScript knows that $state and other runes are only declared within svelte files?

A quick search indicates something like this should be possible:

declare module '*.svelte' {
  declare global {
    function $state<T>(initial: T): T;
    function $state<T>(): T | undefined;
  }
}

Reproduction

Have a Svelte project, create a non-svelte file (e.g. db.ts) and use $state, e.g. const hello = $state(0)

Logs

System Info

System:
    OS: macOS 15.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 93.50 MB / 18.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 23.10.0 - ~/.asdf/installs/nodejs/23.10.0/bin/node
    Yarn: 4.9.2 - ~/.asdf/installs/nodejs/23.10.0/bin/yarn
    npm: 10.9.2 - ~/.asdf/plugins/nodejs/shims/npm
    bun: 1.2.16 - /opt/homebrew/bin/bun
  Browsers:
    Safari: 18.5
  npmPackages:
    svelte: ^5.34.1 => 5.34.1

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions