TypeScript skipLibCheck still checking node_modules libs
Asked Answered
A

4

50

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.

Astroid answered 25/1, 2020 at 4:28 Comment(13)
Possible duplicate: https://mcmap.net/q/239456/-how-can-i-disable-all-typescript-type-checkingMatutinal
Hey you, welcome to StackOverflow! We recommend you to follow the guidelines about posting the first topics on StackOverflow: stackoverflow.com/help/duplicatesMatutinal
There is no point in loading the @types definitions using GSAP 3+. They don't support it and GSAP 3 has its own official definitions includedTantalite
@ZachSaucier thanks. How do I unload the @types? I just installed the library and built the project, what do I do now?Astroid
@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.Astroid
docs.npmjs.com/uninstalling-packages-and-dependenciesTantalite
@ZachSaucier do you mean I should npm uninstall gsap? Can you please clarify?Astroid
I don't want to uninstall gsap, I just want it not to be checked by TSAstroid
how are you importing gsap in your code?Cruelty
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/gsapPeacetime
Is this a TypeScript error or webpack/esbuild/babel error?Thunder
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?Nonagon
It is unlikely that skipLibCheck or exclude will fix your issue. Can you share a sample repo reproducing your configuration and the issue?Mcmillan
N
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');
Ninety answered 16/6, 2021 at 8:53 Comment(2)
One of the libraries was broken, this helps me a lot as a temporary solutionMonopolize
you're a savior, thanks for the require trick !Verbosity
R
0

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'
Repent answered 7/7, 2023 at 19:4 Comment(0)
S
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"
  }
}
Soloman answered 18/7 at 9:2 Comment(0)
A
-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
    }
}
Amati answered 1/6, 2023 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.