One of the reasons many people choose TypeScript over Javacript as a development language is the power of TypeScript’s intelligent hints. TypeScript’s smart hints and a host of other features are built on strict type restrictions and smart type judgment, which can expose problems early in the process of writing code and give you the necessary hints when appropriate. Today we are going to solve the intelligent prompt problem in node.js development. We know that modules and methods we write will require attributes to be typed so that we know the type and methods inside them when we use them. However, we have to use a large number of third-party libraries in development, and these libraries are written in JS, so there is no such thing as type. Therefore, we need a file with all of the library’s methods and properties typed as they should be. This file is the D.ts file. This file simply declares library methods and properties as Typescript requires, rather like an iOS.h header. As can be seen from the above, obtaining the D.ts file is the key to solving the intelligent prompts of third-party libraries. In the early days, we used typings, a third-party platform, to obtain D. ts files, which was rather tedious. Besides, the third-party library will be updated from time to time, so our D.ts file needs to be updated along with the third-party library. After Typescript 2.0, d.ts files can be automatically managed and updated using NPM alone.

npm install @types/XXXXX
Copy the code

You can try replacing XXXXX with the library you are using. Currently, most third-party libraries are supported by Types. If you are not using a particular library, this command will suffice for your development needs. When we’re done running this command, the library’s d.ts file will appear in the @types folder under node_modules. A little analysis of this file will give you an idea of what it does and how to write it, so you can also write a d.ts file for any third party library that doesn’t exist in Types.

And then you can go back to your own code and use

import * as Koa from "koa"
Copy the code

Let’s introduce the SI library and start your fun coding.