Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typings polutes global space with DOM types #1285

Closed
yo1dog opened this issue Sep 13, 2021 · 3 comments · Fixed by #1287
Closed

Typings polutes global space with DOM types #1285

yo1dog opened this issue Sep 13, 2021 · 3 comments · Fixed by #1287

Comments

@yo1dog
Copy link
Contributor

yo1dog commented Sep 13, 2021

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.

/// <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/node

Attempting 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 errors

TypeScript 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.

@yo1dog
Copy link
Contributor Author

yo1dog commented Sep 13, 2021

Pull request: #1287

@Azarattum
Copy link

+1 for this one.

It's really annoying!

@github-actions
Copy link

🎉 This issue has been resolved in version 3.2.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants