patterntypescriptCritical
TypeScript export imported interface
Viewed 0 times
importedtypescriptexportinterface
Problem
I use AMD modules and I want to hide a complex interface behind one file that loads several other files and chooses what to expose and how. It works, I use this solution but it feels kinda ugly, mostly with the interfaces.
Usage:
Any better solutions out there? I don't want to put everything into a single file but I want to
import Types = require('./message-types');
import MessageBaseImport = require('./message-base');
export interface IMessage extends Types.IMessage {} // This is an interface
export var MessageBase = MessageBaseImport; // This is a classUsage:
import Message = require('message');
import { * } as Message from 'message'; // Or with ES6 style
var mb = new Message.MessageBase(); // Using the class
var msg: Message.IMessage = null; // Using the interfaceAny better solutions out there? I don't want to put everything into a single file but I want to
import a single file.Solution
There is an export import syntax for legacy modules, and a standard export format for modern ES6 modules:
// export the default export of a legacy (`export =`) module
export import MessageBase = require('./message-base');
// export the default export of a modern (`export default`) module
export { default as MessageBase } from './message-base';
// when '--isolatedModules' flag is provided it requires using 'export type'.
export type { default as MessageBase } from './message-base';
// export an interface from a legacy module
import Types = require('./message-types');
export type IMessage = Types.IMessage;
// export an interface from a modern module
export { IMessage } from './message-types';Code Snippets
// export the default export of a legacy (`export =`) module
export import MessageBase = require('./message-base');
// export the default export of a modern (`export default`) module
export { default as MessageBase } from './message-base';
// when '--isolatedModules' flag is provided it requires using 'export type'.
export type { default as MessageBase } from './message-base';
// export an interface from a legacy module
import Types = require('./message-types');
export type IMessage = Types.IMessage;
// export an interface from a modern module
export { IMessage } from './message-types';Context
Stack Overflow Q#30712638, score: 321
Revisions (0)
No revisions yet.