Typescript: Support export default const

Created on 25 Sep 2017  路  6Comments  路  Source: microsoft/TypeScript



TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
2.6.0-dev.20170921

Code

export default const a = 'a';

Expected behavior:
Compile succ

Actual behavior:
Compile error

Out of Scope Suggestion

Most helpful comment

We recommend taking these features to TC39 instead.

All 6 comments

This is actually not currently supported by the ECMAScript specification.

Currently, export default may only be combined with the various declaration forms for class and function.

There was a fair amount of discussion about this a while ago on https://esdiscuss.org. I'll try to find the thread and link it.

Hear is the thread, it dates to 2013 and covered the topic in depth. https://esdiscuss.org/topic/why-is-export-default-var-a-1-invalid-syntax

We recommend taking these features to TC39 instead.

What about enums? export default enum is fine, but export default const enum doesn't work.

For others having the same problem:

You can just do it like that:

export const a = 5;
export default a;

The issue is that the syntax could be ambiguous, that's why anything link export default const a = 5; won't work. Example:

// nonsense
export default const a = 5, b = 5, c = 5;

Read more here, as @aluanhaddad already pointed out: https://esdiscuss.org/topic/why-is-export-default-var-a-1-invalid-syntax

Was this page helpful?
0 / 5 - 0 ratings