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) {} }
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.
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.
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.
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.
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 { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With