Reputation: 100130
I have a project with a folder in it, each with a module that should have a particular interface:
project/
modules/
A/index.ts
B/index.ts
C/index.ts
each of the index.ts files, should adhere to a certain interface. Something like this:
export const foo = ...
export const bar = ...
how can I declare that each index.ts file must export a certain interface? In other words, I need to tell TypeScript that module.exports
for each of these index.ts files must adhere to a certain interface.
I filed an issue for this with TypeScript / DefinitelyTyped on Github: https://github.com/Microsoft/TypeScript/issues/19554
Upvotes: 0
Views: 223
Reputation: 31833
In general, specifying types that modules must implement is not yet supported, but you can use a convention such as you suggested in the GitHub issue that you linked from.
Well, it actually does work with export forms but you are using invalid export syntax (export {} as MyInterface
) irrespective of the presence of types.
One way to write this would be
export interface MyInterface {
id: number;
name:string
}
const m: MyInterface = {
id: 1
}
export = m;
We might think to write it more concisely as
export = {id: 1} as MyInterface;
which is valid syntax but acts as a type assertion, not an implementation requirement, such that
export = {} as MyInterface;
typechecks as well. This makes the first form preferable.
Upvotes: 1