27
votes

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.

1
Hey you, welcome to StackOverflow! We recommend you to follow the guidelines about posting the first topics on StackOverflow: stackoverflow.com/help/duplicates - Francis Rodrigues
@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
I don't want to uninstall gsap, I just want it not to be checked by TS - dj1
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

1 Answers

4
votes

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');