TypeScript typings in npm @types org packages

Ronald Zarīts picture Ronald Zarīts · May 31, 2016 · Viewed 59.1k times · Source

I've noticed there's an npm organization @types, which contains typing packages, but can't find any documentation on it. How are these meant to be used?

Is it meant to be used with typings tool? If so, how to install them? For instance, there's a @types/openlayers package, but typings search npm:openlayers returns nothing.

Is it meant to be used separately from the typings tool? E.g. installed directly with npm?

Answer

randominstanceOfLivingThing picture randominstanceOfLivingThing · Oct 12, 2016

As of TypeScript 2.0, typings is no longer required. The npm organization is an entity to setup a developers team. I believe Microsoft setup the @types organization in npm and added the TypeScript developer team to the organization. Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool as per the docs.

In addition, to there is another way to add types to your packages:

In your package.json

If your package has a main .js file, you will need to indicate the main declaration file in your package.json file as well. Set the types property to point to your bundled declaration file. For example:

{
    "name": "awesome",
    "author": "Vandelay Industries",
    "version": "1.0.0",
    "main": "./lib/main.js",
    "types": "./lib/main.d.ts"
}

Note that the "typings" field is synonymous with "types", and could be used as well.

Also note that if your main declaration file is named index.d.ts and lives at the root of the package (next to index.js) you do not need to mark the "types" property, though it is advisable to do so.

Regarding searching types

For the most part, type declaration packages should always have the same name as the package name on npm, but prefixed with @types/, but if you need, you can check out https://aka.ms/types to find the package for your favorite library.

From - http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html

But when I did npm search @types/openlayers, I did not get any results. But doing the search from the web interface did return me the results. So I guess npm search does not search across organizations.