Typescript: Cannot 'export default' abstract, ambient class or interface

Created on 9 Jul 2015  路  17Comments  路  Source: microsoft/TypeScript

export default abstract class B {

}

By the way, the same with

export default declare class B {

}

Seems to be a parser bug

Suggestion help wanted

Most helpful comment

We should just allow these. It's a strange inconsistency, the workaround is _more_ verbose, and the error gives you no help getting to the workaround.

All 17 comments

In what situations would one need to make an ambient class a default export?

These two are by desing. We have felt that the export default syntax is already long enough, so no modifiers are needed. for a workaround use:

declare class  C {}
export default C;

We should just allow these. It's a strange inconsistency, the workaround is _more_ verbose, and the error gives you no help getting to the workaround.

Yeah, discussed with @danquirk and @RyanCavanaugh offline; in a .d.ts file, a default exported ambient class makes sense.

I don't think it's that unreasonable to support the original syntax.

should also cover interfaces (https://github.com/Microsoft/TypeScript/issues/3914):

export default interface User {
    wpUserID: string;
}

And namespaces: export default namespace foo { ... } (#7407)

Accepting PRs for this.

Does this mean a namespace can be anonymous?

export default namespace {
    export var foo = 100;
}

What about an interface?

export default interface {
    foo: number;
}

No other changes except for allowing the export default ... ... modifier combinations that were previously illegal.

I would prefer support for export default enum and export default const enum too. (#3320)

I'm OK with leaving it as is if the reasoning is that interfaces, et al, are not vaid EcmaScript and #3917 is implemented.

Unsupported export default patterns:

  • [X] export default abstract class C { } Added by #14657
  • [ ] export default declare class C { }
  • [X] export default interface I { } Added by #16040
  • [ ] export default enum E { }
  • [ ] export default const enum E { }
  • [ ] export default namespace N { }
  • [ ] export default type T = { }

@mhegazy what about export default type foo = {} ?

@mhegazy for full symmetry with JS export forms, consider (strawman syntax) export default as ...:

JS | TS
---|---
export const x = 123 | export type T = number
export { x } | export { T }
export class Foo {} | export interface Foo {}
export default class Foo {} | export default interface Foo {}
export default 123 | export default as number

not sure what that means..

export default as number

but if it is a .d.ts, we chose not to have a special syntax for this, and the recommendation here is to use

declare cosnt _t: number;
export default _t;

Yeah, sorry, it is a bit ambiguous. Currently, you can sort of export a type as default with:

type T = number;
export default T;

By analogy of const ~= type:

export const x = 123;
export type T = number;
export default x;
export default T;
export default 123;
export default ???; // can't put type expressions here, only type names, due to ambiguity

The motivating case would would be something like:

// Action.ts
export default as
  | { type: 'login', name: string, password: string }
  | { type: 'logout' }
  | ...

where currently you would need to bury the export at the bottom of the file.

I picked export default as <type> as strawman simply because as already suggests a type on RHS, but it doesn't really imply the right thing, you're right. Perhaps export type default <type>?

Really look forward to #18628! Thanks @NaridaL. 馃憦馃憦 馃憦

Was this page helpful?
0 / 5 - 0 ratings