Why the limitation on exporting an interface by default in TypeScript?

battmanz picture battmanz · May 16, 2015 · Viewed 43.5k times · Source

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?

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?

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

Answer

Shaun Luttin picture Shaun Luttin · Jul 24, 2017

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 
{

}