45

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.

Sarju
  • 173
  • 2
  • 9
dj1
  • 451
  • 1
  • 4
  • 4
  • Possible duplicate: https://stackoverflow.com/a/54571297/3332734 – Francis Rodrigues Jan 25 '20 at 05:28
  • Hey you, welcome to StackOverflow! We recommend you to follow the guidelines about posting the first topics on StackOverflow: https://stackoverflow.com/help/duplicates – Francis Rodrigues Jan 25 '20 at 05:30
  • 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 included – Zach Saucier Jan 25 '20 at 20:00
  • @ZachSaucier thanks. How do I unload the @types? I just installed the library and built the project, what do I do now? – dj1 Jan 26 '20 at 15:14
  • 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. – dj1 Jan 26 '20 at 15:17
  • https://docs.npmjs.com/uninstalling-packages-and-dependencies – Zach Saucier Jan 26 '20 at 16:10
  • @ZachSaucier do you mean I should npm uninstall gsap? Can you please clarify? – dj1 Jan 26 '20 at 16:54
  • 4
    I don't want to uninstall gsap, I just want it not to be checked by TS – dj1 Jan 26 '20 at 16:54
  • how are you importing gsap in your code? – thedude Mar 10 '21 at 20:28
  • 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` – cdimitroulas Apr 26 '21 at 10:23
  • Is this a TypeScript error or webpack/esbuild/babel error? – gilamran May 18 '21 at 07:31
  • 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? – RedGuy11 May 18 '21 at 20:56
  • It is unlikely that [skipLibCheck](https://www.typescriptlang.org/tsconfig#skipLibCheck) or [exclude](https://www.typescriptlang.org/tsconfig#exclude) will fix your issue. Can you share a sample repo reproducing your configuration and the issue? – PopGoesTheWza Jun 04 '21 at 21:55

4 Answers4

12

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');
sr9yar
  • 4,850
  • 5
  • 53
  • 59
0

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
    }
}
gnom1gnom
  • 735
  • 7
  • 15
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'
Chris
  • 2,766
  • 1
  • 29
  • 34
0

Had Similar Issue, but after adding the skipLibCheck in the tsconfig file, it was fixed.