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

使用Vite+TS+vite-plugin-dts生成类型文件出错求助

解决vite-plugin-dts仅生成转发式index.d.ts的问题

排查方向1:校验TypeScript配置

确保tsconfig.json配置符合类型生成要求:

  • 开启compilerOptions.declaration: true,确保TypeScript会生成类型文件
  • 检查include数组覆盖了所有需要生成类型的源码文件,示例:
    {
      "include": ["src/**/*"]
    }
    
  • 避免exclude数组误排除src/lib下的文件

排查方向2:优化vite-plugin-dts配置

添加更明确的路径配置,帮助插件正确解析源码:

dts({
  insertTypesEntry: true,
  rootDir: resolve(__dirname, 'src'), // 指定源码根目录
  outputDir: resolve(__dirname, 'dist'), // 指定类型输出目录
  skipDiagnostics: false, // 开启类型诊断,排查潜在类型错误
})

排查方向3:检查入口文件导出逻辑

确认src/lib/index.ts的导出方式:

  • 避免多层链式转发导出(如export * from './components/xxx'嵌套过多),尽量直接导出需要对外暴露的成员:
    export { Editor } from './components/Editor'
    export type { EditorProps } from './components/Editor'
    
  • 确保每个导出的模块自身都有正确的类型定义(无语法或类型错误)

排查方向4:清理缓存后重新构建

  • 删除dist文件夹和node_modules/.vite缓存目录
  • 重新执行vite build,避免缓存导致的类型生成异常

排查方向5:验证依赖版本兼容性

  • 升级vite、vite-plugin-dts、typescript到最新稳定版
  • 确保三者版本互相兼容(比如vite-plugin-dts对Vite 4+的支持更完善)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 06:25:00