Typescript: `export default` enums causes error

Created on 31 May 2015  ·  4Comments  ·  Source: microsoft/TypeScript

export default
enum Foo {
  Bar, Baz
}

causes

test.ts(2,1): error TS1109: Expression expected.

error. (file is test.ts)

export
enum Foo {
  Bar, Baz
}

compiles successfully.

By Design

Most helpful comment

This is by design - ES6 just supports export default class ... and export default function ..., and so we support those, but we didn't add enums. If you find yourself sorely missing this, you can suggest that we allow enums to be declared as default exports as part of a separate issue, but as a trivial workaround, you can instead just write the following:

enum Foo {
  Bar, Baz
}

export default Foo;

All 4 comments

This is by design - ES6 just supports export default class ... and export default function ..., and so we support those, but we didn't add enums. If you find yourself sorely missing this, you can suggest that we allow enums to be declared as default exports as part of a separate issue, but as a trivial workaround, you can instead just write the following:

enum Foo {
  Bar, Baz
}

export default Foo;

cc @RyanCavanaugh

I'd prefer to have this default export a first class citizen of the language, too. Same fo const enums.

If the workaround is trivial then I belive it should be integrated with the language, so one isn't required to remember this workaround every time it occurs.

I would also vote for making export default enum a first-class citizen. While it's true that the workaround provided by @DanielRosenwasser is trivial, it certainly was not obvious to me beforehand. If the intention/goal is to steer people away from using export = and instead prefer using export default, then it would helpful to apply export default consistently for classes, interfaces, and enums.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weswigham picture weswigham  ·  3Comments

seanzer picture seanzer  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments