Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is .d.ts file and why it is needed

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?

like image 677
Max Koretskyi Avatar asked Apr 25 '15 16:04

Max Koretskyi


People also ask

What is the purpose of a D TS file?

d. ts is the type definition files that allow to use existing JavaScript code in TypeScript. declare function sum(a: number, b: number): number; From now on, we can use the function in TypeScript without any compile errors.

What is D TS file Salesforce?

d. ts files will allow the compiler to check the code even though the original TypeScript source for the library is not present. This same approach could be also used to break up a large project into several smaller projects that use the declaration files to establish the interface between them.

What Is A TypeScript definition file?

TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and properties of built-in types like string or function , top-level names like Math and Object , and their associated types.


1 Answers

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++.

like image 56
Darin Dimitrov Avatar answered Sep 20 '22 08:09

Darin Dimitrov