【问题标题】:How to stop TypeScript compiler from reporting compilation errors in symlinked modules如何阻止 TypeScript 编译器报告符号链接模块中的编译错误
【发布时间】:2020-04-28 22:36:27
【问题描述】:

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

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

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

TSForkWebpackPlugin 报告了 create-react-app 的类似问题,但我使用 reportFiles 配置选项使用 react-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"]
}

【问题讨论】:

    标签: typescript create-react-app symlink monorepo pnpm


    【解决方案1】:

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

    当然这不是超级优化,但由于我们一直在使用 Babel 将 TS 编译为 cjs 和 esm,所以无论如何我们现在节省了大量时间,同时还拥有观看模式和 CRA 提供的其他内容多包宽。对于每个非增量的本地包,每次更改后都需要重新构建之前,例如在许多层面上冗长且不便。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-16
      • 2022-01-15
      • 2014-02-26
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多