在运行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
文件夹中出现了很多未定义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[];
如何解决此问题?
答案 0 :(得分:1)
您的依赖项取决于DOM库,因此您必须将其添加到项目中:
"lib": [
"es6",
"es2017",
"DOM"
],
原因:当您省略compiler options中的库选项(搜索--lib
)时,默认的库将被注入您的项目中。
默认的库是:
对于ES6目标:
对于ES5目标:
一旦开始定义库,就必须更明确,因为只会注入那些库。