簡體   English   中英

添加lib ES2017時如何解決node_modules文件夾中的鍵入問題(未定義文檔)?

[英]How to resolve typing issues (undefined Document) in node_modules folder when adding lib ES2017?

在運行tsc@2.5.2的舊式打字稿項目中,我想從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文件夾中出現很多類型錯誤,這些錯誤是未定義Document的,例如:

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

原因:當在編譯器選項中省略library選項(搜索--lib )時,默認的libaries將注入到您的項目中。

默認庫為:

  • 對於ES6目標:

    • DOM
    • ES6
    • DOM.Iterable
    • ScriptHost
  • 對於ES5目標:

    • DOM
    • ES5
    • ScriptHost

一旦開始定義庫,您必須更加明確,因為只有定義的庫將被注入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM