简体   繁体   English

TypeScript 导出导入接口

[英]TypeScript export imported interface

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.我使用 AMD 模块,我想在一个文件后面隐藏一个复杂的界面,该文件加载其他几个文件并选择要公开的内容和方式。 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.我不想将所有内容都放入一个文件中,但我想import一个文件。

There is an export import syntax for legacy modules, and a standard export format for modern ES6 modules:遗留模块有导出导入语法,现代 ES6 模块有标准导出格式:

// 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';

Some more examples besides #c-snover's answer from here .除了来自#C-snover的回答更多的例子在这里 You can put them together.你可以把它们放在一起。

import 'jquery';                        // import a module without any import bindings
import $ from 'jquery';                 // import the default export of a module
import { $ } from 'jquery';             // import a named export of a module
import { $ as jQuery } from 'jquery';   // import a named export to a different name
import * as crypto from 'crypto';       // import an entire module instance object

export var x = 42;                      // export a named variable
export function foo() {};               // export a named function

export default 42;                      // export the default export
export default function foo() {};       // export the default export as a function

export { encrypt };                     // export an existing variable
export { decrypt as dec };              // export a variable as a new name
export { encrypt as en } from 'crypto'; // export an export from another module
export * from 'crypto';                 // export all exports from another module
                                        // (except the default export)

In my case particularly, I had to 'declare' the interface instead of exporting it.特别是在我的情况下,我必须“声明”接口而不是导出它。

declare interface IFluxStoreSyncOptions{
  namespacedKey: string;
}

In order to use the interface as a type in another file like so:为了将接口用作另一个文件中的类型,如下所示:

export function FluxStoreSync(options: IFluxStoreSyncOptions){
}

This way you don't have to export and import the interface.这样您就不必导出和导入界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM