question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript requires `dom` library even for `node` only projects.

See original GitHub issue

Environment

Name Version
msw 0.21.2
node 12.16.3
OS macOS catalina 10.15.7 (19H2)

Problem

The msw TypeScript compilation requires dom library even for node only projects.

tsconfig.json

  {
    "compilerOptions": {
      "module": "commonjs",
      "target": "es6",
      "outDir": "out",
-     "lib": ["es6", "dom"],
+     "lib": ["es6"],
      "sourceMap": true,
      "strict": true,
    },
    "include": ["src/**/*"]
  }

Current behavior

When the dom library is missing, TS can’t resolve types like Request, URL, and ServiceWorker.

node_modules/msw/lib/types/utils/handlers/requestHandler.d.ts:9:82 - error TS2304: Cannot find name 'RequestInit'.

9     fetch: (input: string | MockedRequest<DefaultRequestBodyType>, requestInit?: RequestInit) => any;
                                                                                   ~~~~~~~~~~~

node_modules/msw/lib/types/utils/handlers/requestHandler.d.ts:13:10 - error TS2304: Cannot find name 'URL'.

13     url: URL;
            ~~~

node_modules/msw/lib/types/utils/handlers/requestHandler.d.ts:14:13 - error TS2304: Cannot find name 'Request'.

14     method: Request['method'];
               ~~~~~~~

Expected behavior

TypeScript should compile msw without dom library when used only for a node project.

Screenshots

Example project: https://gitlab.com/viktomas/showcase-msw-bug

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Oct 17, 2020

@timdeschryver, I’ve tried that but that quickly leads to re-annotating half of the DOM. There are a ton of things missing ( fetch, XMLHttpRequest) or existing in a incompatible form (Event) between the DOM and NodeJS.

I’m thinking if we can output an entirely separate build for NodeJS that would derive from the same source, guaranteeing its integrity, but would have its own bundling and types. That way we could annotate the same utilities differently for NodeJS. That still doesn’t solve the issue that those utilities quite heavily rely on certain DOM types.

And I’ve completely forgot about the entry points. Even if we configure it the way described above the users won’t still see the difference, as they import utilities from msw and not msw/node. At this point I’m completely lost.

0reactions
kettanaitocommented, Mar 9, 2022

The dom lib is a dependency of msw. You should either add it to your project ("lib": ["dom"] in tsconfig.json) or use skipLibCheck altogether (not recommended). The library cannot assume in which environment it’s going to be used—that is runtime information. Thus, the library depends on the maximum possible scope of types, which includes dom.

I’m open to suggestions on how to annotate this better but I think there are no options currently available in TypeScript to have this support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable DOM library type definitions in nodejs project VSCode
js TypeScript project. I have configured TypeScript using the configuration shown in the snippet below. Despite explicitly setting the "lib" ...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >
Publishing Node modules with TypeScript and ES modules
TypeScript will even support ES3 if you really need maximum ... if I was building a quick side project for myself and only...
Read more >
How to Setup a TypeScript + Node.js Project | Khalil Stemmler
We talk about a lot of advanced Node.js and TypeScript concepts on this blog, ... testing libraries, and even the browser DOM api....
Read more >
A Complete Guide to Using TypeScript in Node.js - Better Stack
Unlike Deno, Node.js does not support TypeScript natively, so additional work is required to bring type checking to the runtime so that Node.js...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found