162

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?

1
  • 17
    Thank you for this question. Oct 22, 2016 at 1:08

3 Answers 3

124

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.

4
  • Great answer! Minor edits would be nice, but this does a much better job of explaining the current state. Oct 12, 2016 at 15:02
  • Since you specify a single file in your package.json under the types property, I noticed you're talking about bundled declaration file. What would be the easiest way to achieve this in an automated build system like gulp?
    – Nicky
    Nov 28, 2016 at 15:15
  • Do I have to add the "main"field if the main is named index.js?
    – Knu
    Dec 28, 2018 at 9:05
  • 2
    Many thanks for this "Note that the "typings" field is synonymous with "types", and could be used as well" May 12, 2019 at 8:47
45

Announcement on the TypeScript blog answers this: The Future of Declaration Files

Summary:

The @types npm organization is for obtaining type definitions with npm. Using these type definitions is a feature is coming in TypeScript 2.0.

This will replace the current projects/tools such as typings and tsd, though these will continue to be supported for some time.

4
  • 1
    Hey Ron, So i'm trying to get these setup, but it doesn't seem like VSCode is realizing I have them installed. After using something like core-js w/ npm i @types/core-js --save, I can see it's in the folder, but the project still throws errors for things. Is there something else I should be doing? Do I need to let tsconfig be aware of @types or something? Like Sean said, there's no documentation anywhere for types yet :( Jul 13, 2016 at 15:53
  • @MarkPieszak you have to be sure VSCode is using TS2.0. See how to install TypeScript locally and use it in VSCode here: github.com/Microsoft/TypeScript/wiki/Nightly-drops Jul 15, 2016 at 9:54
  • Yup that was it @nino.porcino thank you :) tsdk needed to be pointed to a 2.0beta install of TS Jul 15, 2016 at 13:13
  • at least for some team projects we're updating (and also in other GH projects we saw) one should also update tsconfig.json with a "types" array property listing those module names, e.g. "types": ["core-js"]
    – superjos
    Oct 16, 2016 at 13:25
10

This is going to be a feature that is rolled out in Typescript 2.0. This provides type support for UMD Modules/Libraries and their respective definitions.

See (Built-in support for UMD module definitions) to get a better understanding of the issues currently with ambient typings.

1
  • Thank you! This is useful, but I don't see the GitHub issue discussing the @types npm org. Is the @types org specifically for UMD /// <reference scenario? Will the TypeScript compiler automatically look for types under node_modules/@types (as it looks for modules in node_modules when 'moduleResolution': 'node')? What does this mean for typings - it also handles ambient dependencies. Is it related in any way? Jun 9, 2016 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.