When working on a legacy TypeScript project running [email protected], I encountered the need to access methods from ES2017, such as the array.includes
method.
To enable this, I made changes to my tsconfig.json
file. Initially, it looked like this:
{
"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"
]
}
After the necessary modifications, it now appears as follows:
{
"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"
]
}
The only change was in the lib
section, but after implementing this adjustment, I started encountering numerous type errors within the node_modules
directory related to 'Document' being undefined. Examples of these errors include:
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'.
...And so on.
If you are facing a similar issue, there are steps that can be taken to resolve it.