-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Omitting "dom" from lib should enforce excluding lib.dom.d.ts (for nodejs) #43990
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
Comments
The flip side is common, and super annoying as well: A browser-only project will include a lib which works in both browser and node, and accordingly includes |
There are absolutely a lot of problems in this area that we're trying to fix, but the proposed solution isn't acceptable for a variety of reasons. It'd be a huge breaking change in code that is intentionally set up with lib references in lieu of a tsconfig lib, and breaks the symmetry that a Moreover, this doesn't actually fix anything, because as soon as you do this, you'll see compilation errors in the .d.ts files. Sad to say, but if there were a simple solution like this that actually worked, we'd have done it by now. |
Not if the |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
For this particular issue, see DefinitelyTyped/DefinitelyTyped#41425. But I agree it would be good to catch this in the compiler, instead of silently having dom types leak into a project. |
This can be also very annoying when you have some types with identical names available in your global scope. In my project, I have a global class |
Currently facing this when using Express, whose exported |
Encountered this in our code base as a result of the We have a frequently used entity called We'd expect something like this to fail since the function doSomething() {
console.log(document) // Should show not defined error
} This compiles without incident. |
@RyanCavanaugh Please consider provide config for user to override type reference.
|
@pilaoda all that's going to do is introduce new errors into your build |
With react-native@0.71.7, Without adding "lib":["dom'] it is throwing error to include dom in lib but on adding lib option in tsconfig is actually causing d.ts errors in my case. Any comments? |
Given that we can't tell This is causing a headache for trying to use Node's native |
|
Bug Report
🔎 Search Terms
avoid dom lib, compile without dom, exclude lib.dom.d.ts
Related to:
🕗 Version & Regression Information
⏯ Playground Link
Reproducing the problem requires to have dependencies installed, which makes it impossible to reproduce on Playground as far as I can see.
Instead I can offer a self-contained minimal reproduction example here.
💻 Code
In a nodejs backend application, the following code is supposed not to compile:
because in nodejs it requires an explicit
import { performance } from "perf_hooks"
in contrast to browser code.The
tsconfig.json
specifieslib: ["es6"]
, which according to the docs (and various SO questions) should result in not adding dom types to the compilation.In addition the project also has npm-installed third party dependencies, in particular
@types/superagent
.🙁 Actual behavior
The code compiles successfully.
In fact, we just had our first production backend crash, because one
performance.now()
slipped through without an explicit import, which clearly should have been caught by the compiler.🙂 Expected behavior
The code should not compile, as it does before having installed
@types/superagent
.Thoughts
The reason why this happens is that the
@types/superagent
contains a triple slash directive in their type definitions:/// <reference lib="dom" />
Note: Such problems can also be caused transitively. In our case, the culprit was actually hidden behind
npm install --save-dev @types/supertest
which transitively brought in the problematic dependency.Can we simply blame these type definitions and convince the maintainers to "fix" that? Probably not. Having a look at the type definitions, what they do makes sense. The library supports both node and browser contexts, and in a browser context certain types (mainly appearing in union types anyway) are e.g. valid input types. Also, the problem could possibly come up in many more dependencies, i.e., another (future) dependency could break the "strict nodejs types" at any time.
Clearly, third party dependencies should not cause false positive compilations in a non-browser context. Perhaps the most sensible solution would be to consider the
/// <reference lib="dom" />
only for the compilation of that particular type definition file, but not to "hoist" it into the compilation of the user code. I.e., if the user omits"DOM"
from thelib
setting it should be respected for all user code.The text was updated successfully, but these errors were encountered: