Check out a free preview of the full Production-Grade TypeScript course:
The "dtslint Setup" Lesson is part of the full, Production-Grade TypeScript course featured in this preview video. Here's what you'd learn in this lesson:

Mike configures dtslint to test the TypeScript declaration files for style and correctness. This helps guard against compatibility regressions.

Get Unlimited Access Now

Transcript from the "dtslint Setup" Lesson

>> Let's set up these tools. The first one is called Dtslint. And I'm introducing it first not because it's best, but it's the oldest and the most established one. It also happens to be the one that definitely-typed uses. So if you're at all involved with fixes to ambient type information, you will find yourself needing to work with Dtslint.

[00:00:23] It's already installed in the project. So what we need to do here is look at the README very carefully because the setup process is slightly elaborate. And if you like, this is a good kinda of thing to just watch me do if you feel like learning from me is a little bit more important than following directly yourself.

[00:00:49] So you wanna read this README very carefully. Because this tool is not strictly designed for exactly what we're using it for, but it will work. I'm gonna create a folder in my test folder called types-pts.lint. And inside this folder, I'm gonna create three things, index.dts, tsconfig, and a tslint file.

[00:01:20] We haven't talked about tslint because it's deprecated. Used to be sort of the best in class linter for TypeScript. But like some of the documentation tools I mentioned, it is very tightly coupled with the compiler API. So it always tended to lag behind new compiler releases and just the upkeep required to keep it working was very high.

[00:01:48] It's one of these things where we stand on the shoulders of everything that came before us. And so it didn't do anything bad. It's expensive to keep working on it. So Dtslint announced that they're voluntarily deprecating their project and they're channeling their efforts into the ESLint stuff that we have already been using today.

[00:02:14] The testing tool we're using is built on top of TSLint and it is a project maintained by Microsoft and is used for the Microsoft-owned definitely-typed project. So I am pretty confident in its stability, given that it needs to keep working or the ecosystem will have big problems. So I'm just copying this TSLint JSON and I'm gonna copy this tsconfig.

[00:02:54] All right, now a couple things to look at here. In this tsconfig, we have a paths property. So this is designed to allow you to refer to this project that you're testing in the natural way that it would be referred to by users. So you're importing from your disk folder, not from the source folder.

[00:03:16] So in this case, what we're gonna need to do is, A couple things. So first, we need to generate type information because our dist folder doesn't contain any declaration files at this point in time. In fact, I will save us the trouble of having to work with that.

[00:03:49] We can write types for some tests. I'll just wire us up one little declaration file here. I'm just trying not to spend an excessive amount of time on this, cuz you can just look at a working setup and kinda copy it. I do want you to understand what's going on here.

[00:04:13] So we're gonna need another tsconfig here. So let's try this, tsc, outDir is dist-types. Declaration, I think it's this one, declaration only. Hopefully this worked. Now we'll just do another config file. That way you don't have to copy down a special command. All right, so like our ESLint 1, I'm just gonna use that as the starting point, right?

[00:05:18] Cuz I wanna refer to the existing tsconfig. We don't need this type thing, we don't need allow.js, what we do need is outDir to be dist-types, just a brand new folder. We want declarations, true. emitDeclarationOnly, true. That's the one I was trying to remember. And we'll just worry about this source folder.

[00:05:47] So we can do TSC P for project. And let's see how this works. And we still have all this trace resolution metadata down there. We have one error. NoEmit is turned on. So we'll turn it off. So we actually emit, and while I'm here, I'm just gonna turn that trace resolution off since it's just muddying the terminal there.

[00:06:32] Did I save this? NoEmit is false. All right, must have just forgotten to save it. So what we should have here is a disk-types folder and it only contains declaration files. So this is great. This is just what we want.