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?
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.
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.
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.
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With