Skip to content

Compiler fails parsing template literal types in TS script blocks (js_parse_error) #36

@pheuter

Description

@pheuter

Summary

Svelte compiler fails to parse TypeScript template literal types in <script lang="ts">, producing js_parse_error Unexpected token. This happens even without runes or $derived.

Repro

Create any Svelte/SvelteKit project (I used sv create + bun install) and put this in a .svelte file:

<script lang="ts">
  let x = ['csv', 'json'] as (`csv` | `json`)[ ];
</script>

Then compile with the Svelte compiler directly:

import { compile } from 'svelte/compiler';
import { readFileSync } from 'node:fs';

const source = readFileSync('./src/routes/+page.svelte', 'utf8');

try {
  const result = compile(source, {
    filename: 'src/routes/+page.svelte',
    generate: 'client',
    dev: true,
    runes: true
  });
  console.log('ok', result.warnings?.length ?? 0);
} catch (e) {
  console.error(e?.code, e?.message);
  if (e?.start) console.error(JSON.stringify(e.start));
  process.exit(1);
}

Actual

js_parse_error Unexpected token with a location pointing at the template literal type.

Expected

Template literal types are valid TS and should parse.

Notes

This also fails when embedded in $derived, but $derived is not required to reproduce.

Environment

  • Svelte: 5.48.2 (from sv create)
  • bun: 1.3.6
  • OS: macOS (arm64)

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