If you want to skip the lecture and get straight to the code, here's the repo with all of the examples.
VS Code, for example, partially solves this problem by automatically importing the type definitions for the any of the libraries you import, if available, behind-the-scenes.
So, whenever VSCode autocompletes your
express methods, there's no magic! It's pulling the the DefinitelyTyped definitions behind the scenes.
However, the VSCode (and other editors) solution is limited because many times there's no way for the editor to know, for instance, the types of parameters a function. See example
There are a few ways to solve this problem. The way you choose will depend on your project requirements, as well as in your preferences and needs.
This approach allows you to define custom definitions in-line. The downside is that the types defined this way can't be reused outside of the file they were defined in.
This approach allows you to add types from existing NPM modules to your code. To implement this, you'll first have to
npm install the appropriate type definitions into your project. For instance, to add
express types, you'll need to
npm install --save-dev @types/express.
You can find any types you need at DefinitelyTyped.
As I've shown, there are several ways to improve type autocomplete by using JSDoc annotations. Of course, if you need actual type checking you'll need to use a statically typed language like Typescript or a tool like flow.
There are also a few other uses for type annotations that I didn't go into detail in this article. Feel free to check them out in this repo.