You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Svelte项目TypeScript编译报错:Unexpected Token问题求助

SvelteKit + TypeScript 组件 Unexpected Token 错误排查

我通过npm create svelte@latest创建SvelteKit项目,启用TypeScript并添加了Tailwind。在组件中声明<script lang="ts">时,出现Unexpected Token错误,错误示例如下:

D:/gits/rpg-board/src/lib/components/FilterBar.svelte:3:12 Unexpected token
 1 |  <script lang="ts">"use strict";
 2 |  class Greeter {
 3 |      greeting;
                  ^
 4 |      constructor(message) {
 5 |          this.greeting = message;

相关代码可参考仓库的8ce8eed599bf042f1d8ccd6db2d57456e6d7f15c提交。

项目初始化完整日志:

PS D:\gits> npm init vite
√ Project name: ... rpg-board
√ Select a framework: » Svelte
√ Select a variant: » SvelteKit
Need to install the following packages:
  create-svelte@2.0.0-next.172
Ok to proceed? (y) y

create-svelte version 2.0.0-next.172

Welcome to SvelteKit!

This is beta software; expect bugs and missing features.

Problems? Open an issue on https://github.com/sveltejs/kit/issues if none exists already.

√ Directory not empty. Continue? ... yes
√ Which Svelte app template? » Skeleton project
√ Add Prettier for code formatting? ... No / Yes
√ Add Playwright for browser testing? ... No / Yes

Your project is ready!
✔ Typescript
  Inside Svelte components, use <script lang="ts">
✔ ESLint
✔ Prettier
  https://prettier.io/docs/en/options.html
  https://github.com/sveltejs/prettier-plugin-svelte#options
✔ Playwright
  https://playwright.dev

Install community-maintained integrations:
  https://github.com/svelte-add/svelte-adders

Next steps:
  1: cd rpg-board
  2: npm install (or pnpm install, etc)
  3: git init && git add -A && git commit -m "Initial commit" (optional)
  4: npm run dev -- --open

To close the dev server, hit Ctrl-C

Stuck? Visit us at https://svelte.dev/chat

目前无法正常使用TypeScript,以下是排查方向和解决方法:

  • 升级SvelteKit到稳定版本
    你使用的create-svelte是2.0.0-next.172(beta版本),可能存在兼容性问题。执行以下命令升级依赖:

    npm install @sveltejs/kit@latest @sveltejs/adapter-auto@latest svelte@latest --save-dev
    

    或者直接重新初始化项目使用稳定版。

  • 检查tsconfig.json配置
    确保编译目标支持类字段语法,修改compilerOptions:

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "ESNext",
        "moduleResolution": "bundler",
        "strict": true,
        "skipLibCheck": true
      },
      "include": ["src/**/*", "svelte.config.js"]
    }
    
  • 确认Svelte配置文件正确性
    检查svelte.config.js是否启用了vite预处理,确保配置如下:

    import adapter from '@sveltejs/adapter-auto';
    import { vitePreprocess } from '@sveltejs/kit/vite';
    
    /** @type {import('@sveltejs/kit').Config} */
    const config = {
      preprocess: vitePreprocess(),
      kit: {
        adapter: adapter()
      }
    };
    
    export default config;
    
  • 重新安装依赖
    删除node_modules和package-lock.json,执行:

    npm install
    

    确保所有依赖正确安装,无报错。

  • 修正组件内TS语法
    尝试给类字段添加类型标注,比如将greeting;改为greeting: string;,看是否能通过编译。

内容的提问来源于stack exchange,提问作者Zerg Overmind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:03:30