Skip to content

"Modifiers cannot appear here" in unrelated export type when props don't have explicit type #2832

@Tortoaster

Description

@Tortoaster

Describe the bug

When not declaring an explicit type for props, it seems type declarations cannot be exported.

Reproduction

This works:

<script lang="ts">
	export type ExampleType = 'A' | 'B' | 'C'

	export interface ExampleProps {}

	const {}: ExampleProps = $props()
</script>

But when I remove the : ExampleProps from const {} = $props(), or when I remove the props line altogether (with or without the accompanying interface), I get this error:

Error: Modifiers cannot appear here.
If this is a declare statement, move it into <script context="module">..</script> (ts)
<script lang="ts">
        export type ExampleType = 'A' | 'B' | 'C'
</script>

(side-note: maybe the error can be updated to show <script module>, as the context tag is deprectated)

The export keyword is highlighted. export function gives no error, which is surprising because the effect of the function may differ between different instances of the component. Thankfully, even if exported, functions cannot be imported from other components, but maybe those should give an error.

Possibly related, this does not work regardless of explicitly typed props:

<script lang="ts">
	const allExamples = ['A', 'B', 'C'] as const;
	export type ExampleType = typeof allExamples[number];
</script>

It gives the same error, but it's valid (though a bit obscure) typescript.

As a workaround, everything works properly in module context (by having separate <script> and <script module> blocks). But since we're also allowed to do export interface Props in normal context, I think it would be more intuitive if anything type-related could be exported in normal context as well (and possibly static functions, readonly consts, etc.), as types are inherently equal across all instances of the component.

Logs

System Info

System:
    OS: Linux 6.14 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (24) x64 AMD Ryzen 9 7900X 12-Core Processor
    Memory: 23.70 GB / 30.50 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.18.0 - ~/.nvm/versions/node/v22.18.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.18.0/bin/yarn
    npm: 10.9.3 - ~/.nvm/versions/node/v22.18.0/bin/npm
    pnpm: 10.13.1 - ~/.nvm/versions/node/v22.18.0/bin/pnpm
  Browsers:
    Chrome: 139.0.7258.127
  npmPackages:
    svelte: ^5.0.0 => 5.38.0

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions