10

Why do i need typings.json like below:

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160831021119"
  }
}

for an Angular2 project? I've got:

typings.json provides additional definition files for libraries that the TypeScript compiler doesn't natively recognize.

which, as a newbie I don't clearly understand.

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
Asim K T
  • 16,864
  • 10
  • 77
  • 99
  • Than might help: http://stackoverflow.com/questions/34660265/importing-lodash-into-angular2-typescript-application – rook Sep 11 '16 at 04:19
  • Above question is regarding issue with importing modules to the project. I have used npm as the package manager before, So I understood what his issue is. What I didn't understand is why do I need a typings.json in Angular2 projects. Looks like the file maps typescript to an exact version of some third-party libraries. – Asim K T Sep 11 '16 at 04:28
  • It's not the only way to use typings. General purpose of this file is to tell Typescript that there's something available in global scope from javascript point of view, meaning there will be no errors during execution even if this object is not visible for Typescript at compilation time. Ex: `declare var module: { id: string };` means that there's an object `module` with property `id` implemented somewhere in plain javascript. – rook Sep 11 '16 at 04:53
  • 2
    Watch this video. You can start from about 5:00 https://www.youtube.com/watch?v=4i1nLrqMR14 – rook Sep 21 '16 at 02:51
  • Thanks rook. I think I have found why we need typings.json file. – Asim K T Sep 21 '16 at 05:05

1 Answers1

6

So, I've found this:

Any JavaScript libraries, such as jQuery, the Jasmine testing library, and Angular, extend the JavaScript environment with features and syntax that the TypeScript compiler doesn't recognize natively. When the compiler doesn't recognize something, it throws an error.

So, If we wrote a library which may use by other persons in their projects along with TypeScript, the TypeScript throws an error. To resolve it, we have to write the TypeScript type definition files (.d.ts files) in the library directory.

AngularJS along with most of the libraries already doing this.But, Libraries like "core-js" and "jasmine" do not include d.ts files in their npm packages. Fortunately, either their authors or community contributors have created separate .d.ts files for these libraries and published them in well-known locations. The typings tool can find and fetch these files for you.

So, we have to write typings.json file to get the correct type definition files to run the project smoothly.

Asim K T
  • 16,864
  • 10
  • 77
  • 99