Why am I still getting types from lib.dom.d.ts when I have overridden the lib property in my tsconfig to omit the dom lib?
Asked Answered
P

3

17

I'm having an issue where I'm trying to compile a TypeScript Node project without DOM types, but TypeScript keeps including the DOM types. Here is my tsconfig.json:

{
    "compilerOptions": {
        "lib": [
            "ES6"
        ],
        "target": "es6",
        "module": "commonjs",
        
        "declaration": true,
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true,
    }
}

As you can see, I am using "lib": ["ES6"] which is suppose to get rid of the DOM types. Using tsc --showconfig I can verify that this is the config that I am using. However, VSCode's IntelliSense allows me to write out types for the HTML elements, and, on compilation, I get an error talking specifically about a clash with a type in lib.dom.d.ts, even though the tsconfig is suppose to exclude them (the ellipses are my own):

node_modules/@types/webgl2/index.d.ts:582:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'WebGL2RenderingContext' must be of type (...), but here has type (...).

582 declare var WebGL2RenderingContext: {
                ~~~~~~~~~~~~~~~~~~~~~~

  C:/Users/username/AppData/Roaming/npm/node_modules/typescript/lib/lib.dom.d.ts:16316:13
    16316 declare var WebGL2RenderingContext: {
                      ~~~~~~~~~~~~~~~~~~~~~~
    'WebGL2RenderingContext' was also declared here.

How can the two definitions of WebGL2RenderingContext clash if I am excluding DOM types in my tsconfig?

Popelka answered 27/7, 2020 at 5:26 Comment(0)
F
13

We were running into the same problem. While it may be a different culprit in your case, here is how you could narrow it down:

Create an empty project, take your tsconfig.json and use a minimal code snippet that works only if lib.dom.d.ts is in scope like:

// This works in a browser, but should fail to compile for nodejs, because in
// nodejs, and explicit import is needed.
const time = performance.now();

If this fails to compile you know that your tsconfig.json is good in general.

Now slowly bring in third partly dependencies, i.e., start npm-installing the dependencies that you had in your "broken" project one-by-one, until you reach the point were the compilation suddenly passes.

In our case the problematic dependency was @types/supertest, which in turn had pulled in @types/superagent. The problem with @types/superagent is that it contains the triple slash directive:

/// <reference lib="dom" />

This seems to have the effect that lib.dom.d.ts gets included into the compilation, effectively overriding the "lib": ["ES6"] you have in your tsconfig.json.

Perhaps you can also find the culprit more quickly by specifically looking for that line in your dependencies.

I still don't have a good answer though how to deal with the problem. Clearly installing a dependency should not result in letting the compilation silently pass on cases that should be a compilation error in a non-browser context. I have opened an issue on the TypeScript repo to see if this can be fixed, and there's another issue opened for @types/superagent.

Ferrule answered 7/5, 2021 at 8:3 Comment(3)
Note also related to this question.Ferrule
You can avoid doing this dependency-by-dependency by using explainFilesSyne
Adding "lib": ["ESNext"] helped me in my case, thanks!Ivy
A
3

All @types packages from your package.json are included by default in the project when the "types" compiler option isn't specified.

In this case the error message shows you have @types/webgl2 installed, and you did not specify which types to include, so it included all types by default.

If you'd like to exclude all types you'd set the following in tsconfig.json:

{
  // ...

  "compilerOptions" : {
    // ...

    "types": [],

    // ...
  }
}

And as always with Typescript, after making config changes make sure to reboot the Language Server using the "Restart Typescript Server" command (press F1 in VSCode).

Anthrax answered 20/10, 2021 at 16:44 Comment(1)
what if I want to include the @types packages I've installed but still ignore the DOM types?Backbite
C
-1

Using TypeScript without DOM types (for node)

Your tsconfig is the right way to do this. i.e.

   "lib": [
        "ES6"
    ],

Why you are getting the error

You most likely have another tsconfig.json in your workspace that is being picked up by vscode.

Chuffy answered 27/7, 2020 at 6:17 Comment(2)
I said in the question that tsc --showconfig shows the config that I am using. IE, if it was another tsconfig, a different one would show up there.Popelka
I am experiencing the same problem with "lib": ["esnext"]Plot

© 2022 - 2024 — McMap. All rights reserved.