Types You Don't Control 12 exercises

Understanding skipLibCheck in TypeScript

The option we're looking for is the skipLibCheck option.

Activating skipLibCheck to true instructs TypeScript to bypass the type checking of declaration files. This significantly speeds up the compilation process:

"compilerOptions": {
"target": "ES2022",
"module": "ESNe

Loading solution


00:00 Okay, the option here is skip lib check. I have turned this to true. And what this means is if we go back to our index.t.ts over here, we can see that if I comment this out or actually just set it to false here, then my index.t.ts file is going to start erroring again.

00:18 Boom, there's the error. Okay, but my recommendation, and actually if you just delete this, if you don't have skip lib check true or don't have it mentioned, it defaults to false as well. False is the default value. So why would you ever have this turned on? And why, when you look at all sorts of stuff,

00:37 like all sorts of templates out there, like the Next.js template or the V template, skip lib check is going to be true. Well, I recommend this because what you're doing when you say skip the check true is you're saying skip checking all declaration files, skip checking all the types inside them.

00:56 Now, mostly in your code, you're going to have maybe two or three declaration files. You don't want to be doing a lot of global sort of implementations or global type annotations inside d.ts files. Now, the reason for that is because as soon as you put anything in the global scope, then it sort of slows everything down just a little bit because it needs to make sure

01:17 that everything in the global scope is okay before then parallelizing and checking all the rest of your TypeScript code. The second reason for that is that most of the declaration files that you're going to be consuming are not controlled by you. They are controlled by your node modules. They are in external libraries.

01:35 They're in the .d.ts files for the DOM and for the lib.d.ts. So skip lib check true actually saves TypeScript a ton of time when checking your code base. This can be, it's going to add an extra like 50% onto your type check time. It means your IDE runs like 50% slower.

01:54 Skip lib check true is really useful because it means that your TypeScript actually gets to focus on your code instead of the code that's being imported. And it means too that if there's an error in that code too, because this sometimes happens, like there might be an error in the definitely type types or there might be an incompatibility

02:13 between your TypeScript version and the TypeScript syntax they're using in the library that you're importing. If that happens, you're going to get an error that you can't fix in your code. You can't do it. You're going to have to find some way, horrible way around that. Whereas if you have skip lib check true, you get to just focus on your code

02:32 and upstream errors in other libraries don't affect you. Now, I wish that this config option was different because by turning on skip lib check true, you're also disabling type checking inside your .d.ts files. This is bloody annoying.

02:48 Like what we really want to say is like skip lib check in node modules true like this, but there's no actual config option that lets us do this. This might change in the future, but for now skip lib check true is the best we can do and the benefits outweigh the costs.

03:08 So I recommend turning on skip lib check true unless you are operating an environment where you just have tons of .d.ts files. Usually you're doing something wrong here. You probably shouldn't be using declaration files to like put your own types in unless you're doing like js doc or something,

03:27 which we will get to eventually. So skip lib check true, always stick it on, eat the cost that you have all of these .d.ts files that are not being type checked, but enjoy a speedier TypeScript experience and pray that the TypeScript team think about

03:43 adding in skip lib check in node modules. By the way, for those thinking about this, doing exclude on this actually makes no difference because you can say exclude node modules here, but this makes no difference to whether the .d.ts files are actually type checked weirdly enough. So skip lib check true is the best we can do.