7

I'm reviewing a source code for angular-ui-router and there is the angular-ui-router.d.ts file inside api folder with the following content:

declare module ng.ui {

    interface IState {
        name?: string;
        template?: string;
        templateUrl?: any; // string || () => string
        templateProvider?: any; // () => string || IPromise<string>
    }

    interface ITypedState<T> extends IState {
        data?: T;
    }

I've read that is file is TypeScript type definitions. What is it? Why is it needed?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 1
    Possible duplicate of [About "\*.d.ts" in TypeScript](https://stackoverflow.com/questions/21247278/about-d-ts-in-typescript) – BuZZ-dEE May 29 '18 at 23:29

2 Answers2

7

It's a declaration file:

When a TypeScript script gets compiled there is an option to generate a declaration file (with the extension .d.ts) that functions as an interface to the components in the compiled JavaScript. In the process the compiler strips away all function and method bodies and preserves only the signatures of the types that are exported. The resulting declaration file can then be used to describe the exported virtual TypeScript types of a JavaScript library or module when a third-party developer consumes it from TypeScript.

The concept of declaration files is analogous to the concept of header file found in C/C++.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks, does it mean that they write this code using TypeScript? Or can it be that they just wanted to make information about interfaces easily accessible? Can you also please elaborate a bit on this _The resulting declaration file can then be used to describe the exported virtual TypeScript types of a JavaScript library or module when a third-party developer consumes it from TypeScript._? – Max Koretskyi Apr 25 '15 at 17:36
  • In C/C++ we use header files to expose funtions, structs, classes to external users without reveal our implementation. With TypeScript is the same, external users know only exported types, so they know what are the operations, classes but not the implementation. – Marcus Henrique Apr 25 '15 at 17:43
  • @MarcusHenrique, yeah, I'm curios whether this is by-product of writing using `TypeScript`, or it was generated from JavaScript source files. – Max Koretskyi Apr 25 '15 at 18:16
  • This can be generated in a number of ways (manually, from the TSC compiler, sometimes from JavaScript, etc). It is *needed* in the sense that if you want TypeScript code to refer to this module with auto-completion, type checking, and etc, you need the definition file. If not you could define it as of the "any" type and just use it as in regular JavaScript, that is, without any compiler checking. – zeh Apr 26 '15 at 21:40
2

Here is the definition. The d.ts files describe the shape of 3rd party library, and make the TypeScript compiler know how to deal with this 3rd party code (supply type information to compiler)

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Neo Ko
  • 1,365
  • 15
  • 25
  • Could you elaborate on that? Why is this important? Why can't we just write a class and export it for example? – Shy Agam Mar 31 '18 at 23:14