60

I am using TypeScript with React, and TypeScript is still checking libraries in node_modules folder, although I have "skipLibCheck" set to true in tsconfig.json..

Here's my tsconfig.json (I added the exclude section for troubleshooting, which also didn't work):

{
  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "es6-promise",
      "webpack-env"
    ],
    "lib": [
      "es5",
      "dom",
      "es2015.collection"
    ]
  }, 
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

React version am using is 15.4.2, and TypeScript is installed globally... I had version 3.7.2, and I upgraded it to 3.7.3 because I read somewhere that skipLibCheck doesn't work with 3.7.2 ..

The error I am getting when trying to build the project with gulp is:

Error - typescript - node_modules\gsap\types\gsap-utils.d.ts(97,75): error TS1144: '{' or ';' expected

If I set skipLibCheck to false, and build the project, I'll have MANY more errors. So seems like the skipLibcheck works partially.

Any idea how to solve this? I am still new to TypeScript. Any help would be appreciated.

13
  • Possible duplicate: stackoverflow.com/a/54571297/3332734 Commented Jan 25, 2020 at 5:28
  • 14
    @FrancisRodrigues I don't think it's a duplicate. The question you pointed me to wants to disable type checking all together. For me I want to keep type checking, but I don't want it to happen for GSAP. Plus, the compiler shows me errors on gsap-util.d.ts, how can this be avoided with checkJs:false? I am open to see a duplicate question if it really describes the problem am having, but so far, posts in other questions didn't solve my issue. Thanks. Commented Jan 26, 2020 at 15:17
  • 4
    I don't want to uninstall gsap, I just want it not to be checked by TS Commented Jan 26, 2020 at 16:54
  • 1
    I think what they are saying is that if you installed the @types for GSAP separately you should uninstall those as they come with GSAP already since version three. npm uninstall @types/gsap Commented Apr 26, 2021 at 10:23
  • 1
    I want types. I don't want to have to build my project. To do that, I'm using JSDoc, since it uses comments that runners will ignore. The only CLI I could find that could lint my types against the JSDoc is the Typescript CLI with "noEmit": true and "checkJs": true. But then I'm getting errrors from node_modules/utils/utils.js. I don't want theese errors. How can I remove them? Commented May 18, 2021 at 20:56

4 Answers 4

13

skipLibCheck is not meant to prevent all type checking in node_modules. Although it may work for some projects, but it's just a coincidence. You could say it works partially, true. Here's what it does:

Skip Lib Check - skipLibCheck

Skip type checking of declaration files.

This can save time during compilation at the expense of type-system accuracy. For example, two libraries could define two copies of the same type in an inconsistent way. Rather than doing a full check of all d.ts files, TypeScript will type check the code you specifically refer to in your app’s source code.

A common case where you might think to use skipLibCheck is when there are two copies of a library’s types in your node_modules. In these cases, you should consider using a feature like yarn’s resolutions to ensure there is only one copy of that dependency in your tree or investigate how to ensure there is only one copy by understanding the dependency resolution to fix the issue without additional tooling.

skipLibCheck was introduced in Typescipt 2.0, so upgrading Typescript isn't really a fix. Yet again it may work for some people. Now I had a case when I had to add a library using Typescript 4 to a project using Typescript 3. It was raining errors on build. Having the same version of typescript helped. The version of typescript would be specific to your project here.

The only quick solution I know is to use require instead of import (my project was backend):

import * as lib from 'lib';
const lib = require('lib');
Sign up to request clarification or add additional context in comments.

1 Comment

One of the libraries was broken, this helps me a lot as a temporary solution
1

I had this problem because I was importing from the src directory of a module instead of the dist directory. The package included both directories but dist was the better choice for pulling the type I wanted as it didn't do a bunch of side-effect imports.

Example:

import type { Log } from 'viem/dist/types'

Instead of...

import type { Log } from 'viem/src/types/log'

Comments

0

In my case, the only possible solution was to delete .d.ts files, which were not understood by my TS version, which I also couldn't change

{
  "scripts": {
    "patch:opensea-seaport-js": "bash -c 'find ./node_modules/@opensea/seaport-js/lib -name \"*.d.ts\" -exec rm -f {} + || true'",
    "postinstall": "yarn patch:opensea-seaport-js"
  }
}

Comments

-1

Set the maxNodeModuleJsDepth to zero. The documentation says

Ideally this should stay at 0 (the default)

But this is not true - if you run tsc with --showConfig, in my case it indicates

{
    "compilerOptions": {
        "maxNodeModuleJsDepth": 2
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.