为什么在TypeScript中默认情况下限制导出接口? [英] Why the limitation on exporting an interface by default in TypeScript?

查看:438
本文介绍了为什么在TypeScript中默认情况下限制导出接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TypeScript 1.5 beta,并且试图将接口导出为默认导出.以下代码在Visual Studio和WebStorm中均导致错误:

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:

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?

编辑:感谢您的回答.但是,这引出了一个问题,那么使用ES6模块语法导入接口的可接受的方式是什么?

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?

这有效:

// 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) {}
}

推荐答案

TypeScript v2.4.0允许export default interface. 此处是引入更改的请求请求.

TypeScript v2.4.0 allows export default interface. Here is the pull-request that introduced the change.

我们现在可以同时做这两项:

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 
{

}

这篇关于为什么在TypeScript中默认情况下限制导出接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆