Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) {} } 
like image 309
battmanz Avatar asked May 15 '15 23:05

battmanz


People also ask

Can we export interface in TypeScript?

Use a named export to export an interface in TypeScript, e.g. export interface Person{} . The exported interface can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

What does export default do TypeScript?

Default exports are meant to act as a replacement for this behavior; however, the two are incompatible. TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module.

Should I use default exports?

I prefer to use default exports when the exported component is only going to be imported once, or if a file exports one thing. A good example would be a Router component.

What is the use of default in export?

Export Default is used to export only one value from a file which can be a class, function, or object. The default export can be imported with any name. //file functions.


1 Answers

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  {  } 
like image 79
Shaun Luttin Avatar answered Oct 18 '22 08:10

Shaun Luttin