Automating retrieval of TypeScript definition files with TSD


In previous posts, I have described how definition files can be retrieved via the Definitely Typed website, which leads you to their GitHub repository. This is all well and good if we need to retrieve a small number of definition files, but it can be a hassle if we want to get several definition files.

Automating definition file retrieval

Thankfully, there is a node package that helps retrieve definition files in a more automated way, So let’s go ahead and install this package, known as tsd:

$ npm install -g tsd

Querying definition files

Now that we have tsd installed, we can run queries to see if we can find definition files:


Installing definition files

Let’s go ahead and give it a shot with a definition file. To install the definition file for, say, mongoose, we run the command:

$ tsd query mongoose --action install

If it all goes well we should get the following:


By default, you will find the definition file in typings/mongoose/mongoose.d.ts.

Tracking all installed definition files

We can take this all a step further and create a tsd.json file to track what definitions we have installed. If this route is chosen, it is worth removing all previously installed definition files and proceed in the following manner:

Create the tsd.json:

$ tsd init

Opening the json file will give us a few settings:

  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {} 

The fields of interest are as follows:

  • path: Path of where the definition files will be installed.
  • bundle: Path to a .d.ts file that contains references to all installed definition files.

To install a definition file, we can run the command:

$ tsd query mongoose --save --action install

Opening the file tsd.json we get the following:

  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
    "mongoose/mongoose.d.ts": {
      "commit": "e6a11b3cf228eacc13006558e6c9903ac7b4975b"

Looking at the file tsd.d.ts, we can see references to all definition files:

/// <reference path="node/node.d.ts" />
/// <reference path="mongoose/mongoose.d.ts" />

We can update all installed definition files with the command:

$ tsd update -so



We have successfully been able to automate the retrieval of definition files. We can query, install and update definition files for use in TypeScript projects.



4 thoughts on “Automating retrieval of TypeScript definition files with TSD

  1. That’s a good start but unless you are going to store you .tsd files in your source code repo, every developer has to run `tsd query mongoose –save –action install` when they are setting up their environment. Can you not give a listing somewhere that specifies which libraries you want definitions for and have a grunt task or something that reads that file and downloads all the type definition files for you? Basically something like npm’s package.json but for .tsd files.

    • Hi Dave,
      Thanks for your comment. The listing can be provided via tsd.json. When you have all your definition files set, run “tsd install” and you’re all set. I imagine there is a way to do this via grunt, but I’ll have to dig a little further. Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s