Quick Tip – Generating a TypeScript Declaration File

@mit $ingh. September 11, 2017 Comments

If you are using TypeScript, you probably want to use 3rd party libraries like jQueryrequire.js or sammy.js. In order to get the environment Intellisense and design time checks, you need to import the libraries *.d.ts files into your project. The *.d.ts files are declaration files that declare to TypeScript design time the exposed objects, functions, classes and interfaces included in a script. Without using the declaration files you will get design time errors since TypeScript compiler won’t recognize the libraries and their content.

There is a repository in GitHub that includes many 3rd party libraries declaration files which is called DefinitelyTyped and I encourage you to check it out.

Generating TypeScript Definition File

Sometimes you have the need to generate your own TypeScript declaration file whether it is because you’ve created a reusable component for your project or you developed an open source TypeScript library. 
So how you can achieve that? by using the TypeScript compiler of course. 
The TypeScript compiler includes the –declaration flag which indicate that you want to generate a declaration file for you TypeScript file. Here is an example of running the compiler with that flag:

tsc --declaration myFile.ts

Summary

TypeScript complier can help you generate your script declaration file. You just need to compile your file with the –declaration flag.

  • TypeScript
  • ts-declaration-file