首页 > 解决方案 > 如何阻止 TypeScript 编译器报告符号链接模块中的编译错误

问题描述

我有一个由 rush.js 控制的 monorepo,PNPM 作为包管理器。

我曾经将所有共享模块预编译成cjs, esm,dts目标。但是这种方法有一些缺陷,所以我决定将它们保留为未触及的来源,并将它们的主要条目设置package.json"main": "./src/index.ts|x". 同时,我曾经react-app-rewired告诉 Webpack 只编译那些node_modules使用 babel 的符号链接库,并且一切正常。杰斯特也很高兴。

我遇到的问题是,当我tsc出于某种原因运行编译器时,编译器会深入到符号链接的本地包并报告很多问题(即使运行它们的编译器也没有任何问题tsc)。

TSForkWebpackPlugin报告了类似的问题,但我使用配置选项create-react-app忽略了它们,并认为这是插件站点上的某种错误,但似乎不是。reportFilesreact-app-rewired

我添加了各种exclude喜欢的 glob 模式,**/node_modules/@namespace/**但这些都不起作用。 也有吗?node_modules/@namespace/**node_modules/@namespace"skipLibCheck": true

tsconfig.json的供参考

{
  "compilerOptions": {
    "incremental": true,
    "baseUrl": "src",
    "downlevelIteration": true,
    "lib": ["esnext", "dom", "dom.iterable"],
    "module": "esnext",
    "target": "esnext",
    "sourceMap": true,
    "allowJs": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": false,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "skipLibCheck": true,
    "noEmit": true,
    "preserveSymlinks": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "strict": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": ["src"]
}

标签: typescriptcreate-react-appsymlinkmonorepopnpm

解决方案


目前,解决这个问题的唯一方法是让tsc符号链接项目是通过添加"types": "./types/index.d.ts"或类似方式编译的库,并发出真实的声明。然后,我假设因为他认为这是一个图书馆skipLibCheck开始工作并且你不再遇到问题了。

当然它不是超级优化的,但是由于我们一直在将 TS 编译到cjsesm使用 Babel,所以无论如何我们现在都节省了大量时间,同时还拥有监视模式和 CRA 多包范围内的其他东西。在每次更改每个本地包之后都需要重新构建之前,这不是增量的,例如在许多级别上很长且不方便。

有关实施细节的任何问题,请问我。


推荐阅读