Currently, there is a limitation in TypeScript where you cannot specify that a module's exports adhere to a specific type, nor can you annotate the import statement on the consuming side. Although feature requests have been raised for these functionalities (see microsoft/TypeScript#420 and microsoft/TypeScript#38511), it is uncertain when or if these changes will be implemented anytime soon. You can still show your support by giving a thumbs up on those issues.
However, one workaround could be to create an assignment somewhere in your codebase that would only work if the module complies with the specified type.
For instance, you could include test code like this in your project:
async function checkModule() {
let checkedModule: Module = await import('./file');
}
If the assignment is successful, it means the module conforms to the type Module
. If not, it will indicate which exported members from 'file.ts' do not meet the requirements of Module
.
While this workaround may not be ideal since the type check is done separately from the exporting module, it is still better than having no type check at all.
You can view a working example on Stackblitz here.