在添加lib es2017时,如何在node_modules文件夹中解决键入问题(未定义的文档)



在运行tsc@2.5.2的Legecy打字稿项目中,我想从ES2017访问方法,例如。array.includes方法。

因此,我从中编辑了我的tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "pretty": true,
    "outDir": "dist",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "node_modules/@types",
    "src/**/*.ts",
    "tests/**/*.ts",
    "cli.ts"
  ]
}

{
  "compilerOptions": {
    "target": "ES6",
    "lib": [
      "es6",
      "es2017"
    ],
    "module": "commonjs",
    "pretty": true,
    "outDir": "dist",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "node_modules/@types",
    "src/**/*.ts",
    "tests/**/*.ts",
    "cli.ts"
  ]
}

基本上,只有lib部分更改,但是现在我在node_modules文件夹中遇到了很多类型错误,该文档不确定,例如:

157     before(content: Document[], ...contents: any[]): Cheerio;
                        ~~~~~~~~
node_modules/@types/cheerio/index.d.ts(157,21): error TS2304: Cannot find name 'Document'.

161     insertBefore(content: Document): Cheerio;
                              ~~~~~~~~
node_modules/@types/cheerio/index.d.ts(161,27): error TS2304: Cannot find name 'Document'.

239     parseHTML(data: string, context?: Document, keepScripts?: boolean): Document[];
                                          ~~~~~~~~
node_modules/@types/cheerio/index.d.ts(239,39): error TS2304: Cannot find name 'Document'.

239     parseHTML(data: string, context?: Document, keepScripts?: boolean): Document[];

如何解决此问题?

您的依赖项取决于DOM库,因此您必须将其添加到项目中:

"lib": [
      "es6",
      "es2017",
      "DOM"
],

原因:当您省略编译器选项中的库选项(搜索--lib(时,默认性诽谤将被注入您的项目。

默认libs是:

  • 对于ES6目标:

    • dom
    • ES6
    • dom.iterable
    • scripthost
  • 对于ES5目标:

    • dom
    • ES5
    • scripthost

一旦开始定义库,就必须更加明确,因为只有定义的库会被注入。

相关内容

  • 没有找到相关文章

最新更新