Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript export imported interface

Tags:

typescript

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.

like image 516
Gábor Imre Avatar asked Jun 08 '15 15:06

Gábor Imre


People also ask

How do I export import interface?

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.

How do I export a imported function in TypeScript?

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.

What is export {} in TypeScript?

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.


1 Answers

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'; 
like image 171
C Snover Avatar answered Sep 20 '22 18:09

C Snover