TypeScript 导出导入接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30712638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:40:44  来源:igfitidea点击:

TypeScript export imported interface

typescript

提问by Gábor Imre

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.

我使用 AMD 模块,我想在一个文件后面隐藏一个复杂的界面,该文件加载其他几个文件并选择要公开的内容和方式。它有效,我使用这个解决方案,但感觉有点难看,主要是接口。

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 importa single file.

有没有更好的解决方案?我不想把所有东西都放在一个文件中,但我想要import一个文件。

回答by C Snover

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

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

回答by Gábor Imre

Some more examples besides #c-snover's answer from here. You can put them together.

除了来自#C-snover的回答更多的例子在这里。你可以把它们放在一起。

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)

回答by Dean Martin

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.

这样您就不必导出和导入界面。