typescript 如何强制 tsc 忽略 node_modules 文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51634361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:33:40  来源:igfitidea点击:

How to force tsc to ignore node_modules folder?

node.jstypescripttsctsconfig

提问by Daniel Santos

I'm using tsc build tasks. Unfoutunately I'm always getting the same errors from the node modules folder

我正在使用 tsc 构建任务。不幸的是,我总是从节点模块文件夹中收到相同的错误

Executing task: .\node_modules\.bin\tsc.cmd --watch -p .\tsconfig.json <
node_modules/@types/node/index.d.ts(6208,55): error TS2304: Cannot find name 'Map'.
node_modules/@types/node/index.d.ts(6215,55): error TS2304: Cannot find name 'Set'.
node_modules/@types/node/index.d.ts(6219,64): error TS2304: Cannot find name 'Symbol'.
node_modules/@types/node/index.d.ts(6225,59): error TS2304: Cannot find name 'WeakMap'.
node_modules/@types/node/index.d.ts(6226,59): error TS2304: Cannot find name 'WeakSet'.
10:13:18 - Compilation complete. Watching for file changes.

I already added the directory to the ignore at tsconfig.json

我已经将目录添加到 tsconfig.json 中的 ignore

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "strict": false,
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
  },
  "include": [
    "src/*"
  ],
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

What I'm doing wrong? What should I do in order to ignore those errors?

我做错了什么?我应该怎么做才能忽略这些错误?

I'm using VsCode and tsc Version 2.9.2

我正在使用 VsCode 和 tsc 版本 2.9.2

回答by Daniel Swiegers

Quickfix is to skip the check

Quickfix 是跳过检查

{
  "compilerOptions": {
    "skipLibCheck": true
  },
}

回答by Jeff Tian

I met this issue with [email protected]and fixed by upgrading it to 3.7.3.

我遇到了这个问题[email protected]并通过将其升级到3.7.3.

Notice with [email protected], the skipLibCheckdoes not take effect. By upgrading typescriptthe skipLibCheck: trueworks.

注意[email protected]skipLibCheck不生效。通过升级typescriptskipLibCheck: true作品。

回答by Mic

Add an empty "types" option in "compilerOptions":

在“compilerOptions”中添加一个空的“types”选项:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "strict": false,
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
    "types": []
  },
  "include": [
    "src/*"
  ],
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

From https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

来自https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

@types, typeRoots and types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.

...

Specify "types": [] to disable automatic inclusion of @types packages.

Keep in mind that automatic inclusion is only important if you're using files with global declarations (as opposed to files declared as modules). If you use an import "foo" statement, for instance, TypeScript may still look through node_modules & node_modules/@types folders to find the foo package

@types、typeRoots 和类型

默认情况下,所有可见的“@types”包都包含在您的编译中。任何封闭文件夹的 node_modules/@types 中的包都被认为是可见的;具体来说,这意味着 ./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/ 等中的包。

...

指定 "types": [] 以禁用 @types 包的自动包含。

请记住,仅当您使用具有全局声明的文件(而不是声明为模块的文件)时,自动包含才重要。例如,如果您使用 import "foo" 语句,TypeScript 可能仍会通过 node_modules 和 node_modules/@types 文件夹查找 foo 包