DEV Community

Linas Spukas
Linas Spukas

Posted on

TypeScript: Type Definition Files

When working with TypeScript, you can use JavaScript modules freely; create, export, import your own, or install third-party libraries. Whenever you install an external module, TypeScript doesn't know anything about it. And this is a problem for the language, as it cannot do its job - check your code for errors. When you import a module into the TS file from a JavaScript library, you will likely see a warning, saying that a module doesn't have a definition file. In this post, I want to briefly explain what it is, why TS needs it, and where to get one if it is not included in the module.

Why Definition Files Are Important

TypeScript's purpose is to analyze your code for possible errors. For that, it needs to know what types are you working with, what functions and methods you are using, and what kind of arguments they accept and return. Without this information, TypeScript can't check your code. So when you install and import a third-party library, TypeScript doesn't have information about the types used in these modules.
We need to add a definition file to help TypeScript to know about the existing properties, functions, arguments, and return values used in the third-party modules. These files include information about the types used in the module.
Sometimes definition files are included in the libraries by default. In that case, you will not see an error when hovering over the imported module name, and you don't need to add it manually.

How To Add Type Definition Files

All definition files are publicly available and placed in the Definitely Typed project in Github. It includes generated types for most of the popular libraries out there. To add a definition file to your project, you need to install an npm module containing @types/{library_name} name. For example, to add a definition file for a lodash library, you would install a package: npm install @types/lodash. To check correct naming for the library types, search the npm repository.
After the installation of the type definition files, a warning for the imported module should be gone.

Benefits Of Type Definitions

Type definition file for a module can be accessed by hovering over the module import in a code editor and pressing command + click (on Mac). You will be redirected to the file containing all the information about the current module: methods, objects, classes, values, properties, and their respective types, everything in TypeScript. It can be used as a source of documentation, to check what type a function will return or arguments accept.
Another advantage is the autocomplete of the module. You initiate a class of the module, and the editor lists all the functions and properties.

Summing Up

The purpose of type definition files is to describe all the functions, properties, and values of the modules. By installing them, we help TypeScript to check and analyze our code for possible errors, and it serves as a source of documentation for the used module.

Discussion (0)