How does one add a typing to typings.json for Typescript in Visual Studio 2015?

Zachary Scott picture Zachary Scott · Apr 14, 2016 · Viewed 33.6k times · Source

I have to ask because this is driving me crazy. I see the npm way of installing typings on Google, but Angular2's tutorial has one add a typings.json file then it added the typings folder and downloaded d.ts files from DefinitelyTyped automatically. I tried this with jquery but it didn't download. I also tried rebuilding the project, which I would expect the package.json to include the commands to add additional typings.

Here's my scripts from the package.json file:

"scripts": {
  "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
  "tsc": "tsc",
  "tsc:w": "tsc -w",
  "lite": "lite-server",
  "typings": "typings",
  "postinstall": "typings install"
}

Here's the typings.json file I tried. es6-shim and jasmine downloaded.

{  "ambientDependencies": {
   "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
   "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
   "jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
}}

It's probably something simple like not having what appears to be a checksum after the hashtag. Where would I find the correct checksum, or what command do I need to add to package.json to retrieve the typings upon compile, or what am I doing wrong?

Here's another example of adding a line to the typings.json file and then it installs the d.ts files for you. Scroll down until you see Manual Typings

Answer

Alex picture Alex · Apr 15, 2016
  1. Make sure you have npm installed
  2. Open up your console of choice (e.g. command prompt or powershell)
  3. Navigate to your project folder

Using only npm (TypeScript 2 and later):

  1. npm install --save @types/jquery

    Done: See this for more info.

Using typings (Typescript before v.2):

  1. Make sure you have typings installed, if not run npm install typings --global
  2. Write typings install dt~jquery --save --global

    This should update your typings.json file and download the definition files.

    In the above example for typings, 'dt~' means that it should look for jquery in the DefinitelyTyped repository, the default is 'npm'. The syntax has changed slightly from version 0.x to 1.0, the flag --global was previously --ambient.