-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Same issue superagent had with their typings: DefinitelyTyped/DefinitelyTyped#41425 (comment)
The dom reference triple-slash directive in /@types/index.d.ts causes all DOM types to be included in the global ambient declarations which breaks type checking.
Line 2 in 9cd2e43
| /// <reference lib="dom" /> |
See: https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-lib-
This means that Node.js projects that import node-fetch can reference DOM global types and values (window, document, Element, ChildNode, etc.) without typing errors.
This line don't seem to be needed as nothing from the DOM library is referenced. Can we just delete this line? I tried doing so, and TypeScript had no complaints.
Reproduction:
Simple TypeScript project that does NOT include DOM library.
echo '{
"compilerOptions": {
"lib": ["ES2015"]
}
}' > tsconfig.json
npm i node-fetch @types/nodeAttempting to access the window global throws a type error as expected.
echo "window.alert('hi');" > index.ts
tsc
# index.ts:1:1 - error TS2304: Cannot find name 'window'.Same as above except we import node-fetch and the type error disappears unexpectedly.
echo "import {} from 'node-fetch';
window.alert('hi');" > index.ts
tsc
# no errorsTypeScript reports the DOM library is being loaded due to a reference in node-fetch's type declaration file.
tsc --explainFiles
# ../../.nvm/versions/node/v14.17.6/lib/node_modules/typescript/lib/lib.dom.d.ts
# Library referenced via 'dom' from file 'node_modules/node-fetch/@types/index.d.ts'As you can see, simply importing node-fetch breaks type checking because lib.dom.d.ts is loaded due to the reference from node-fetch/@types/index.d.ts. This can also be observed in VSCode where DOM types are shown in autocomplete suggestions.