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 in index.d.ts cause dom types to be available globally in any package using cross-fetch #70

Closed
tupelo-schneck opened this issue Aug 6, 2020 · 12 comments

Comments

@tupelo-schneck
Copy link

Since index.d.ts contains /// <reference lib="dom" />, all the lib.dom types are imported globally for anyone who uses cross-fetch: microsoft/TypeScript#33901

That means if I'm working in a TypeScript project where I don't want to have lib.dom available (which is surely true of anyone using cross-fetch) I have to somehow override the cross-fetch typings.

I suggest depending on @types/node-fetch instead and using

export * from 'node-fetch';
export { default } from 'node-fetch';

to re-export the declarations from it.

@jstewmon
Copy link
Contributor

I came here after running into this issue: graffle-js/graffle#206

Referencing the TS dom lib in packages which also target node causes problems...

DefinitelyTyped/DefinitelyTyped#12044 (comment)

In addition to the disjoint set of globals, there are also conflicting global types, like the return types of timer functions.

@calebfrieze
Copy link

calebfrieze commented Feb 16, 2021

Any movement on this? Ended up moving to TypeScript on a node.js project that uses this library.RequestInit gives me an error when i pass in an agent stating that agent does not exist on RequestInit. Seems like this is also due to the fact that your index.d.ts contains /// <reference lib="dom" />.

@carlpaten
Copy link

@lquixada would you welcome a PR addressing this?

@lquixada
Copy link
Owner

@LilRed sure! The ideal PR would be all the type definitions for fetch, Headers, Request and Response from this document: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts.

@jstewmon
Copy link
Contributor

👋 Hi, after running into this issue several times across various projects, I recently wrote a script to automate the extraction of dom lib types for the SimpleWebAuthn project.

In my [minuscule] free time, I've been working on making a standalone CLI out of it in case other projects wanted to take a similar approach. I just published an initial version under the package name ts-graft. It is still very basic, but it is functional and I believe can make the task of extracting type definitions from lib files much easier than doing so by hand.

I'm not necessarily trying to promote the project - I've barely had any time to work on it as is, but if you want to try it out for this, I think it will make the chore very easy.

Of course, if you do have any feedback on what I've got, it's welcome. 🤗

@lquixada
Copy link
Owner

hey @jstewmon! this tool looks amazing! will def take a look at it! Thanks!

@lquixada
Copy link
Owner

Closing this as it was addressed on PR #93 .

@mshwery
Copy link

mshwery commented Apr 13, 2021

Noting here that the closing PR got reverted, so this issue still stands.

If the type namespaces match, can we avoid using /// <reference lib="dom" /> altogether? Consumers should already be including lib: ['dom'] as needed in browser-based targets, while excluding it in Node targets (since it's inaccurate and can cause type conflicts).

In the browser tsconfig, the declarations should merge, right?

@bali182
Copy link

bali182 commented Mar 7, 2022

Hello, just came across this issue. It's still present in 3.1.5.

May I ask why @jstewmon's PR was reverted?

We could use lib: ['dom'], but if this is used in a node+typescript project, that kind of defeats the purpose of using typescript, as now all usages of browser apis will compile, even though they will fail at runtime.

@ab-pm
Copy link

ab-pm commented Nov 7, 2023

Please reopen after 3fce389 due to #101

@ladal1
Copy link

ladal1 commented Oct 3, 2024

The 4.0.0 version still contains the dom import which can cause errors when used casually with node, such reference is too strong for use in types right now. This cange needs to be reviewed again

@antogyn
Copy link

antogyn commented Dec 17, 2024

I just ran into this issue too, running tsc --explainFiles led me to this repository.

Until this is fixed, as a workaround you can set the version to 3.1.1 in the resolutions property of package.json:

"resolutions": {
  "**/**/cross-fetch": "3.1.1"
}

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

No branches or pull requests

10 participants