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.
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 class
Usage:
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 interface
Any better solutions out there? I don't want to put everything into a single file but I want to import
a single file.
Use a named export to export an interface in TypeScript, e.g. export interface Person{} . The exported interface can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.
Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With