Future of Declaration Files

2 minute read

When I have started to use TypeScript I have looked at Definitely Typed to see how many people where writing declaration files (.d.ts) and then guidelines from Microsoft to see how to do it. There was then the utility tsd, but now many stuff has changed.

Evolution at Glance

TSD Package Manager

TSD is a package manager to search and install TypeScript definition files directly from the community driven >DefinitelyTyped repository.

TSD

A tool has to be installed via npm to fetch the package and start to configure a file named tsd.json that holds all the declaration (or definition) files. Basically tsd is like npm with similar commands able to manage defintion dependencies used by TypeScript compiler to transpile .ts files and by type guard to perform runtime check.

A type guard is some expression that performs a runtime check that guarantees the type in some scope.

TypeScript

Typings

Some months go, looking at the GitHub repository, I have discovered the project has been deprecated in favor of typings. So typings is an evolution of tsd with some advantages explained here by blakeembrey.

Basically the previous tool has been improved into a more generic package manager that acts like npm, then it still requires to be installed. Old projects have to be updated to migrate from tsd if previously used. Slighter different syntax needs to be learnt.

@Types

Recently I was experimenting Angular2 Command Line Interface and I have noticed that generating a new project the folder typings was not created, something missing? Not at all, TypeScript 2.0 has improved once again and a new property types has been kicked in the tsconfig.json.

So looking at the tsconfig.json schema definition we can find

types: {
  "description": "Type declaration files to be included in compilation. Requires TypeScript version 2.0 or later.",
  "type": "array",
  "items": {
    "type": "string"
  }
}

The evolution is explained in this post from Daniel Rosenwasser Program Manager on TypeScript.

In a nutshell

Getting type declarations in TypeScript 2.0 will require no tools apart from npm. The future of declaration files

Really a good news! we can rid off some extra entities in our project such as:

  • another package managers then npm;
  • json file for type declarations;
  • configure tsconfig.json to exclude files from typings folder;
  • the famous ///reference at the beginning of each .ts file.

Usage

Very simple:

npm install --save @types/lodash

From there you’ll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global >code.

The future of declaration files

No more additional configuration or references at the beginning of each file.

Please refer to TypeSearch search engine to find a type definition and how to install it via npm.

At first sight I could say the project is cleaner, less utilities and, may be, a more consistent way to manage .d.ts files. I hope a definitive path has been defined (at least for a couple of years) in order to have all the dependencies centralized in one place.