TypeScript skipLibCheck 仍在检查 node_modules 库

dj1*_*dj1 24 typescript reactjs gsap

我在 React 中使用 TypeScript,TypeScript 仍在检查 node_modules 文件夹中的库,尽管我在 tsconfig.json 中将“skipLibCheck”设置为 true ..

这是我的 tsconfig.json(我添加了排除部分进行故障排除,这也不起作用):

{
  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "es6-promise",
      "webpack-env"
    ],
    "lib": [
      "es5",
      "dom",
      "es2015.collection"
    ]
  }, 
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}
Run Code Online (Sandbox Code Playgroud)

我使用的 React 版本是 15.4.2,TypeScript 是全局安装的......我有 3.7.2 版,我将它升级到 3.7.3,因为我在某处读到过 skipLibCheck 不适用于 3.7.2 ..

尝试使用 gulp 构建项目时遇到的错误是:

Error - typescript - node_modules\gsap\types\gsap-utils.d.ts(97,75): error TS1144: '{' or ';' expected
Run Code Online (Sandbox Code Playgroud)

如果我将skipLibCheck 设置为false,并构建项目,我将有更多错误。所以似乎 skipLibcheck 部分工作。

知道如何解决这个问题吗?我还是 TypeScript 的新手。任何帮助,将不胜感激。

sr9*_*yar 13

skipLibCheck is not meant to prevent all type checking in node_modules. Although it may work for some projects, but it\'s just a coincidence. You could say it works partially, true. Here\'s what it does:

\n
\n

Skip Lib Check - skipLibCheck

\n

Skip type checking of declaration files.

\n

This can save time during compilation at the expense of type-system\naccuracy. For example, two libraries could define two copies of the\nsame type in an inconsistent way. Rather than doing a full check of\nall d.ts files, TypeScript will type check the code you specifically\nrefer to in your app\xe2\x80\x99s source code.

\n

A common case where you might think to use skipLibCheck is when there\nare two copies of a library\xe2\x80\x99s types in your node_modules. In these\ncases, you should consider using a feature like yarn\xe2\x80\x99s resolutions to\nensure there is only one copy of that dependency in your tree or\ninvestigate how to ensure there is only one copy by understanding the\ndependency resolution to fix the issue without additional tooling.

\n
\n

skipLibCheck是在 Typescipt 2.0 中引入的,因此升级 Typescript 并不是真正的解决方案。再次,它可能对某些人有用。\n现在我遇到了一个情况,我必须将使用 Typescript 4 的库添加到使用 Typescript 3 的项目中。构建时出现了大量错误。拥有相同版本的打字稿很有帮助。Typescript 的版本将特定于您的项目。

\n

我知道的唯一快速解决方案是使用require而不是import (my project was backend):

\n
import * as lib from \'lib\';\nconst lib = require(\'lib\');\n
Run Code Online (Sandbox Code Playgroud)\n