Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should TypeScript Interfaces Be Defined in *.d.ts Files

TypeScript newbie question. In our project, we are using some external JavaScript libraries where we needed to add *.d.ts files. I understand this use case and the reason why we needed to do this.

But, for our own interfaces that we are defining, one of my developers suggested that we define them in *.d.ts files so that we could have access to the interface type without importing it into the modules that need to use it.

For example, we wanted to create an interface for an "error-first callback" function so that we could reuse it in many areas.

So instead of this...

export function helloWorldEventually(callback: (err: Error, result: any) => void) {   callback(null, 'Hello World'); } 

We could define an interface for the error first callback like this...

export interface ErrorFirstCallback {   (err: Error, result: any): void; } 

And use it like this...

export function helloWorldEventually(callback: ErrorFirstCallback) {   callback(null, 'Hello World'); } 

At first, I just defined the ErrorFirstCallback interface in ErrorFirstCallback.ts, and imported it in order to reference it.

Another developer suggested we put in in a *.d.ts file and then we would not need to import it in order to reference it.

When should interfaces that we are defining be defined in a *.d.ts file vs a *.ts file.

Thanks!

like image 877
Kevin Avatar asked Jun 17 '16 00:06

Kevin


People also ask

Where do you define interface TypeScript?

In TypeScript, an interface is an abstract type that tells the compiler which property names a given object can have. TypeScript creates implicit interfaces when you define an object with properties. It starts by looking at the object's property name and data type using TypeScript's type inference abilities.

What is the use of D ts file in TypeScript?

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 index D ts in TypeScript?

d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.


1 Answers

Declaration files describe the shape of external JavaScript libraries. For example using jQuery with $ will result in a TypeScript error without declaration files, because $ or JQuery is undefined. Therefor a declaration file creates an interface, so the compiler knows "if this variable is of type JQuery it must have the functions x,y,z"

When creating interfaces for your project, you should put them where ever you like: Within one big interface-file, within an individual file for each interface, or within the file where it may belong to, BUT within a declaration file would be very inconvenient.

Personally i like to have individual files for each module/class/interface. But its just a matter of taste.

The only thing considering creating a declaration file is to give other developers the possibility to use you final JavaScript file(not TypeScript!) in their project.

like image 112
Mick Avatar answered Oct 14 '22 02:10

Mick