Home > Net >  Does TypeScript support export default Enum?
Does TypeScript support export default Enum?

Time:06-06

I have an enum object which I want to make the export default at the top level like that :

export default enum Hashes{

FOO = 'foo',
BAR = 'bar',
}

I got this error :

Module parse failed: Unexpected token (1:15) File was processed with these loaders: [02:54] MABROUK, Sahnoun (external - Project)

  • ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
  • ./node_modules/@ngtools/webpack/src/ivy/index.js

I tried this way :

export enum Hashes{
    
    FOO = 'foo',
    BAR = 'bar',
    }

and it seems work only if I import Hashes as alias in all my components like that :

import {Hashes} from ... which is a huge change in my project !

any solution ?

CodePudding user response:

This is how ES6 works.

enum Hashes {
    
    FOO = 'foo',
    BAR = 'bar',
    }

export default Hashes;

Exporting it as const? Concerning the default export, there is only a single default export per module. A default export can be a function, a class, an object or anything else. This value is to be considered as the "main" exported value since it will be the simplest to import.

export const enum Hashes {

FOO = 'foo',
BAR = 'bar',
}

CodePudding user response:

export default expects an expression/function/class after it, you can read here more Export MDN

This is not due to TS, this is due to error in your statement. You can simply defined you enum first and then export it default.

enum Hashes {
 FOO = 'foo',
 BAR = 'bar',
}

export default Hashes
  • Related