为什么在 TypeScript 中默认导出接口有限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30270084/
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
Why the limitation on exporting an interface by default in TypeScript?
提问by battmanz
I'm using TypeScript 1.5 beta, and I'm trying to export an interface as the default export. The following code causes an error in both Visual Studio and WebStorm:
我正在使用 TypeScript 1.5 测试版,并且我正在尝试将接口导出为默认导出。以下代码会在 Visual Studio 和 WebStorm 中导致错误:
export default interface Foo {...}
However, the following code works fine:
但是,以下代码工作正常:
interface Foo {...}
export default Foo;
Is this by design, is it a bug, or am I doing something wrong?
这是设计使然,是错误还是我做错了什么?
EDIT:Thank you for your answer. It begs the question, however, so what is the accepted way to import an interface using the ES6 module syntax?
编辑:谢谢你的回答。然而,它引出了一个问题,那么使用 ES6 模块语法导入接口的可接受方式是什么?
This works:
这有效:
// Foo.ts
export interface Foo {}
// Bar.ts
import {Foo} from 'Foo'; // Notice the curly braces
class Bar {
constructor(foo:Foo) {}
}
But, since that works, why not allow a default export and save the curly braces?
但是,既然这样有效,为什么不允许默认导出并保存花括号呢?
// Foo.ts
export default interface Foo {}
// Bar.ts
import Foo from 'Foo'; // Notice, no curly braces!
class Bar {
constructor(foo:Foo) {}
}
回答by Shaun Luttin
TypeScript v2.4.0 allows export default interface
. Here is the pull-requestthat introduced the change.
TypeScript v2.4.0 允许export default interface
. 这是引入更改的拉取请求。
We can now do both of these:
我们现在可以做到这两个:
// Foo.ts
export interface Foo { }
// Bar.ts
export default interface Bar { }
// Baz.ts
import { Foo } from "./foo";
import Bar from "./bar";
export class Baz implements Foo, Bar
{
}
回答by Juan Pablo
it is not necessary to export the interface
不需要导出接口
// Foo.ts
interface Foo {}
// Bar.ts
class Bar {
constructor(foo:Foo) {}
}