Search code examples
typescripttypingtsc

Typescript: Understanding how the pieces fit - typings, @typings, tsc and DefinitelyTypes


Am I getting this right?

tsc


Its the typescript cli tool and the global version running on my machine.

typings


typings is a few things. A cli tool to install .d.ts files into your project AND the folder that save to inside my project.

For example:

you can search for .d.ts scripts by running:

typings search foo

and add a package:

typings install --global --save dt~foo

then have the typings/index.d.ts reference all my .d.ts definition files

/// <reference path="globals/foo/index.d.ts" />

Then if I wanted to use foo anywhere in my project I could add

 declare var foo: any;
 import foo from "foo";

Definitely Types


Definitely Types files have a .d.ts extension that allow javascript methods to be called in Typescript

@typings


Is a folder located at node_modules/@typings and is the same as the typings folder, but are installed by running npm install @typings/foo and not typings install foo.


My questions are:

  1. why are there two different ways to install typings do this? Is the first method a depreciated method of including Definitely Types files to my project?

  2. Are @typings module still searchable by running?

    typings search foo

  3. How are I make my project aware of my typings, did defining them like /// <reference path="globals/foo/index.d.ts" /> get scrapped?

  4. Do I always/when do I declare my .d.ts files in my project like declare var foo: any;

  5. Do you still need to use the ambient or global flags when installing?


Solution

  • This was a little confusing for me as well as I've spent the last couple months familiarizing with TypeScript...

    tsc

    Correct, this is the TypeScript Compiler which translates your files to JavaScript.

    Typings

    Typings is a cli TypeScript definitions manager which allows you to manage (search, install, uninstall, etc.) definition files (*.d.ts) for JavaScript libraries from a variety of sources.

    DefinitelyTyped

    DefinitelyTyped is a repository of definition files which is one of the sources that Typings allows you to gather/manage files from.

    npm @types

    The good news is with latest release of TypeScript (version 2.0+) you no longer need to use a manager and/or worry about which repositories to gather files from. All of the definitions from DefinitelyTyped are now available to install via npm (example: npm install @types/knockout --save).

    Using this method will download the definition file, save the dependency in your project.json file (if the --save flag is specified) which TypeScript then automatically looks for the definition whenever you require() the module within your code (you no longer need to use /// <reference /> if you use this method).


    The TypeScript website has pretty extensive documentation on project setup and how to use tsconfig.json files to properly setup your application for compilation with or without definition files.