【问题标题】:How to force tsc to ignore node_modules folder?如何强制 tsc 忽略 node_modules 文件夹?
【发布时间】:2019-01-09 02:07:29
【问题描述】:

我正在使用 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.

我已经将目录添加到tsconfig.json的忽略


    {
      "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",
      ]
    }

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

我正在使用 VsCode 和 tsc 版本 2.9.2

【问题讨论】:

  • 这可能会有所帮助:"...为此,编译器需要一个模块的定义,这可以是您自己的代码的 .ts 文件,也可以是 .d。 ts 用于导入的定义文件。如果找到该文件,则无论在前面的步骤中是否排除它,都将包含该文件。" -> github.com/Microsoft/TypeScript/wiki/…
  • 我想要类型。我不想构建我的项目。为此,我使用 JSDoc,因为它使用跑步者会忽略的 cmets。我能找到的唯一可以针对 JSDoc 对我的类型进行 lint 的 CLI 是带有“noEmit”的 Typescript CLI:true 和“checkJs”:true。但后来我从 node_modules/utils/utils.js 得到错误。我不想要这些错误。我怎样才能删除它们?

标签: node.js typescript tsc tsconfig


【解决方案1】:

无论skipLibCheck 是真的,它都不适合我。

【讨论】:

  • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
【解决方案2】:

tsconfig.json内设置"skipLibCheck": true

【讨论】:

    【解决方案3】:

    您可以在命令行上直接执行此操作

    tsc --skipLibCheck
    

    【讨论】:

    • 这更好,因为如果您使用 webpack 插件来验证包含的库类型,至少在 webpack 构建期间会验证库类型
    • 这对我有用(TS 4.4.4)。即使我也在 tsconfig 中指定了它,但显然我明确需要像您的解决方案一样这样做..
    • 编辑:关于我上面的评论;我没有 RTFM,我在 tsc 命令中指定了一个文件,正如文档中明确指出的那样,它将忽略 tsconfig; typescriptlang.org/docs/handbook/…
    【解决方案4】:

    我通过typescript@3.2.1 遇到了这个问题,并通过将其升级到3.7.3 来解决。

    注意typescritp@3.2.1skipLibCheck 不生效。通过升级typescript skipLibCheck: true 可以工作。

    【讨论】:

    • 很有帮助,已升级到最新版本。 3.9.7 并添加了 skipLibCheck
    • 酷更新ts版本
    【解决方案5】:

    Quickfix是跳过检查

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

    【讨论】:

    • 这很有效,因为我正在导入的库只导出它自己的类型,并且我通过使用脚本导入来导入实际文件,以便我可以从 cdn 提供它们。谢谢:)
    • 这也会跳过你对所有声明*.d.tsdd.engineering/blog/…的类型检查
    • skipLibCheck 是否只针对 node_modules 文件夹?
    • @DilipAgheda 不,它还将针对node_modules 之外的任何d.ts 文件。所以这可能不是大多数人想要的。
    【解决方案6】:

    在“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",
      ]
    }
    

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

    @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 包的文件夹

    【讨论】:

    • @MichaelOzeryansky:对不起,我不明白你的意思。我只是在 OP 的 tsconfig.json 中添加了一行
    • @MichaelOzeryansky 这个添加可能会让你的编译运行得更快。更多行不会自动使编译时间变慢。更少的行不会让你的代码运行得更快。这里需要注意的是,由于不再包含他们需要的类型,这将阻止许多项目在没有其他重大更改的情况下进行编译。
    猜你喜欢
    • 2015-02-17
    • 1970-01-01
    • 2020-05-28
    • 2018-01-12
    • 2016-02-13
    • 2013-05-26
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多