TypeScript requires `dom` library even for `node` only projects.
See original GitHub issueEnvironment
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:
- Created 3 years ago
- Reactions:4
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 notmsw/node
. At this point I’m completely lost.The
dom
lib is a dependency ofmsw
. You should either add it to your project ("lib": ["dom"]
intsconfig.json
) or useskipLibCheck
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 includesdom
.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.