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

如何解决VS Code中自定义Vite别名插件引发的TS错误?

解决TS2307模块找不到错误

问题根源

你的TS配置存在两个核心问题:

  1. 继承后的路径解析冲突:le工作区继承_common的tsconfig.json时,baseUrl和paths的相对路径未适配le的目录结构,导致TS无法正确解析@/别名。
  2. include路径冗余错误:_common的tsconfig中使用了../_common/这类错误路径,干扰了TS的文件扫描范围。

修复步骤

1. 修正_common/tsconfig.json

清理_common的配置,让它仅关注自身工作区的文件:

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,
    "baseUrl": "./",
    "paths": {
      "@/*": ["./*"] // 仅指向_common自身文件
    },
    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "allowUnreachableCode": true
  },
  "include": ["*.tsx", "src/**/*", "tokens/**/*"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

2. 调整le/tsconfig.json

在le的配置中继承_common,并覆盖baseUrl、paths和include以适配自身目录:

{
  "extends": "../_common/tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./", // 指向le工作区根目录
    "paths": {
      "@/*": ["./*", "../_common/*"] // 优先扫描le自身,再匹配_common
    }
  },
  "include": ["*.tsx", "src/**/*", "tokens/**/*", "../_common/**/*"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

3. 验证VS Code TS服务版本

确保VS Code使用工作区的TypeScript版本,而非全局版本:

  • 打开任意TS/TSX文件
  • 点击右下角的TS版本号(如5.1.6)
  • 在弹出菜单中选择Use Workspace Version

4. 重启TS服务

按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入TypeScript: Restart TS Server,重启后错误即可消失。

修复逻辑说明

  • 拆分配置后,_common和le的paths规则与自定义Vite插件的解析逻辑完全对齐,保证构建和TS解析行为一致。
  • 修正include路径后,TS能正确扫描到_common中的所有文件,不会遗漏类型声明。
  • 明确baseUrl的指向,避免继承配置时的路径混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:09:52