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!