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

Turbo Monorepo中@typescript-eslint/no-unsafe相关报错求助

解决Turbo Monorepo中@typescript-eslint类型检测报错问题

针对你遇到的从同工作区db包导入repo对象时,ESLint误判为any类型导致@typescript-eslint/no-unsafe-call和@typescript-eslint/no-unsafe-member-access报错的问题,可按以下步骤排查解决:

1. 检查db包的package.json类型导出配置

确保db包的package.json中正确声明了类型入口,让ESLint能读取到完整的类型定义:

{
  "name": "@your-workspace/db",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts", // 必须指向编译后的类型文件
  "files": ["dist"] // 确保dist目录被包含在工作区引用范围内
}

如果db包使用tsup、esbuild等工具编译,要确认编译时生成了.d.ts类型文件,且路径与types字段一致。

2. 配置ESLint的parserOptions指向完整的tsconfig

在项目根目录或使用repo的子包的.eslintrc.cjs中,让parserOptions包含工作区所有相关的tsconfig路径,确保ESLint能读取全局类型信息:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: [
      './tsconfig.json',
      './packages/**/tsconfig.json' // 覆盖所有子包的tsconfig文件
    ],
    tsconfigRootDir: __dirname
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-type-checked'
  ]
};

若子包使用特殊命名的tsconfig(如tsconfig.build.json),需对应修改路径。

3. 配置tsconfig的项目引用

在根目录的tsconfig.json中启用项目引用,关联db包的配置,让TypeScript正确联动工作区类型:

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "declarationMap": true
  },
  "references": [
    { "path": "./packages/db" } // 指向db包的tsconfig所在目录
  ]
}

同时db包的tsconfig.json要开启composite: true,确保类型可被其他包引用:

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

4. 清理缓存并重新编译

缓存可能导致类型信息不同步,执行以下命令清理后重新构建:

# 清理Turbo缓存
npx turbo clean

# 清理TypeScript缓存
rm -rf node_modules/.cache/typescript

# 重新编译整个工作区
npx turbo build

5. 配置ESLint的导入解析器

如果使用eslint-import-resolver-typescript,在.eslintrc.cjs中添加配置,确保它能识别工作区包:

settings: {
  'import/resolver': {
    typescript: {
      project: './packages/**/tsconfig.json'
    }
  }
}

若以上步骤无效,可额外检查:

  • db包中repo对象是否有明确的类型导出(比如export const repo: RepoType,而非依赖隐式推断)
  • 子包的tsconfig是否继承了根配置,导致类型解析逻辑冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 09:01:07