9

I am trying to learn angular 2 with typescript and want to know why and how is this typings file created and used ?

In my project folder I have a typings folder which is generated and file called index.d.ts. I have gone through the documentation of typescript and cant seem to understand why it is needed?

S_developer
  • 252
  • 5
  • 18

2 Answers2

8

Typings describes contract of libraries you use. This allows the TypeScript compiler that what you use exist (classes, properties, ...).

You can install typings from a repository using the typings command or let the compiler find out them leveraging the strategy specified in the tsconfig.json file with the moduleResolution attribute.

For Angular2, they (.d.ts files) are resolved within the node_modules/@angular folder since the framework contains its typings. For other libraries, like Lodash, it's not the case. So you need to install them from the repository.

To define a typings file you can leverage the export declare class clause:

export declare class SomeClass {
  name: String;
  constructor(name?: String);
}
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
3

The typings folder and the index.d.ts are managed by a type definition package manager called typings or possibly its predecessor tsd.

For third-party libraries that do not ship with their own type definitions, the typings package manager is used to install those type definitions into your project.

Martin
  • 15,820
  • 4
  • 47
  • 56