Reputation:
Short and sweet, is it better practice to define my own interfaces with declare or with export?
// i.something.d.ts
export default interface ISomething {
myValue: string;
}
// something.ts
import ISomething from 'i.something';
export class Something implements ISomething {...}
vs.
// i.something.d.ts
declare interface ISomething {
myValue: string;
}
// something.ts
export class Something implements ISomething {...}
Of course, even declare doesn't work ambiently if it has to import another type of its own. (Unless there is a better way to do it)
// i.something-else.d.ts
import SomeBiggerThing from '...';
import SomeKindOfType from '...';
declare interface ISomethingElse extends SomeBiggerThing {
myValue: SomeKindOfType;
}
// something-else.ts
// ISomethingElse isn't available unless I import it, because it imports its own stuff.
import ISomethingElse from 'i.something-else';
export class Something implements ISomethingElse {...}
Upvotes: 22
Views: 12772
Reputation: 12814
Most large/mature TypeScript libraries avoid relying on ambient declarations and instead export
anything that needs to be reused outside of the current file. For example, the Angular codebase has an interfaces.ts
file for each module which is imported into each file that needs to reference the interfaces:
This has a few advantages:
import
statement at the top of the file.import
and export
statements, while ambient type contexts are a TypeScript-only concept.Upvotes: 25