Skip to content

fix: don't emit invalid export on type/interface declarations in the instance script#3078

Closed
spokodev wants to merge 1 commit into
sveltejs:masterfrom
spokodev:fix/svelte2tsx-investigate
Closed

fix: don't emit invalid export on type/interface declarations in the instance script#3078
spokodev wants to merge 1 commit into
sveltejs:masterfrom
spokodev:fix/svelte2tsx-investigate

Conversation

@spokodev

@spokodev spokodev commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #2832

export type/export interface in the instance script keep their export keyword inside the generated render function when they aren't hoisted to the module scope, e.g. when $props() has no explicit type or there are no props at all. export isn't valid on a declaration inside a function body, so TypeScript reports "Modifiers cannot appear here." on an otherwise valid component.

Repro:

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

Adding a typed const {}: Props = $props() makes the error disappear, because the hoisting path then moves the type out to the module scope — which is why this is easy to miss.

Fix: strip the export keyword from top-level type/interface declarations that stay inside the render function (the ones that don't get hoisted), mirroring the existing handling for exported functions and classes in handleExportFunctionOrClass. Hoisted declarations are left untouched so they remain importable from other components.

Adds a regression test.

…e instance script

`export type`/`export interface` in the instance script kept their `export`
keyword inside the render function when they weren't hoisted to the module scope
(e.g. when $props() has no explicit type), producing a "Modifiers cannot appear
here" error. Strip the keyword in that case, mirroring the existing handling of
exported functions and classes.

Fixes sveltejs#2832
@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2aceaa5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte2tsx Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jasonlyu123

Copy link
Copy Markdown
Member

I think removing the modifier will only make it even more confusing. You can't import it, and there is no error message indicating a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants