Is it possible to use export default Enum in TypeScript?

I am facing an issue with exporting an enum object as default at the top level in my code. Here is what I tried:

export default enum Hashes{

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

However, this resulted in an error message:

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 then attempted a different approach:

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

This worked, but required me to import Hashes as an alias in all my components like this:

import {Hashes} from ... which has caused significant changes in my project!

Is there any alternative solution to this problem?

Answer №1

Understanding the functionality of ES6.

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

export default Hashes;

Considering exporting as const? In regards to the default export, each module can only have one default export. The default export can be a function, a class, an object, or any other entity. This value is crucial as it serves as the primary exported value and makes importing easier.

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

Answer №2

Remember, when using export default make sure to follow it with an expression, function, or class
. For more information, you can refer to Export MDN

It's important to note that this issue is not specific to TypeScript; rather, it's a common mistake in your code. To resolve it, simply define your enum first and then use export default.

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

export default Hashes

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with receiving data on $.ajax POST request

If you'd like to check out my website Please use the following login details (case sensitive): Username: stack Password: stack Click on the tab labeled "yourhours." The main goal is to send all input box data to a database. At the moment, I am fo ...

The addition of the woocommerce_add_to_cart action is causing issues with the website's native add to cart

After creating a node express API that listens for post requests from the woocommerce_add_to_cart webhook, I noticed that the payload it receives is not very useful. body:{ action: 'woocommerce_add_to_cart', arg:'098uoijo098920sa489983jk&ap ...

Indicate various file formats within the interface

Is there a way to define multiple file types in an interface? interface App { service: Record<ServiceName, Service> } For example, the ServiceName could be Cart, Product, User, etc. And the Service should be an imported object from a .ts file. ...

Retrieve information in JSON format using AngularJS by fetching data from SQL through PHP

My objective is to retrieve data from a mySql database using PHP, convert it to JSON, and then display it through AngularJS. Although I have successfully completed the steps leading up to this point, I am facing issues with the final step. Below are the de ...

The concatenation function in JavaScript does not seem to be functioning properly with JSON

My attempt to use .concat() in order to combine two objects is resulting in tiles.concat is not a function The following code (in an angular app and written in coffeescript): $scope.tiles = new UI(); $scope.tiles.loadUITiles(); console.log($sco ...

Issue with OpenAI's Rate Limit 429 Restriction

I've been experimenting with this repository in order to implement semantic search for YouTube videos using OpenAI + Pinecone. However, I keep encountering a 429 error at the following step - "Run the command npx tsx src/bin/process-yt-playlist.ts to ...

Declarations for TypeScript in NPM packages

In order to streamline my development process with bun.sh using workspaces as npm packages, I have created a tool available here. However, I am facing two issues: After bun installing the 'core' packages from npm and testing a sample, I encounte ...

Axios: Exception handling does not involve entering the catch method

Implementing a function to adjust a contract name involves making an axios request to the backend API using a specific ID. Upon each execution, a sweetalert prompt is displayed. axios({ url: '/api/contract/' + id, method: 'put ...

I'm curious if it's possible to utilize Raspberry Pi GPIO pins within a JavaScript frontend

Is it possible to utilize Raspberry Pi's GPIO pins in Javascript? Specifically, I am interested in reading the values of the Raspberry Pi PIR sensor without having separate Python and Javascript applications. Ideally, I would like a solution that inte ...

Inability to load a MySQL table using Ajax on the same webpage

I'm facing an issue where I want to load a table from mySql on the same page without getting redirected to another page. The user selects a date range, and upon pressing submit, it should appear in the designated div id tag. However, the functionality ...

Utilizing a series of linked jQuery functions

Is there a more efficient way to write this code snippet? $('#element').html( $('#element').data('test') ); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="el ...

Having trouble getting a React Hook to function properly in NextJS with TypeScript. Let's troubleshoot

I'm currently utilizing NextJS to showcase information fetched from a database in a table format. After retrieving the data, my intention is to leverage the map function to generate table rows and then incorporate them into the table. import React, {u ...

Is there a way to conceal a slice of a pie chart in HighCharts without excluding it from the legend display?

I've been searching everywhere for a solution to this issue, but I just can't seem to pinpoint where I'm going wrong. My goal is to initiate a pie chart using HighCharts with specific slices hidden as if they were "clicked" off in the legen ...

Attempting to call a nested div class in JavaScript, but experiencing issues with the updated code when implemented in

Seeking assistance. In the process of creating a nested div inside body > div > div . You can find more information in this Stack Overflow thread. Check out my JSFiddle demo here: https://jsfiddle.net/41w8gjec/6/. You can also view the nested div ...

Webstorm showcases files with similar content in a distinct manner

In Webstorm, everything is color-coded based on whether it is a function, object, etc. I am working with JavaScript. I have noticed that in two different files, the same line of code looks differently: var Comment = React.createClass({ In file 1: "var" ...

How can I prevent clicks on child links using the :not() selector?

I am working on a script using JQuery where I want to add a click handler to a div while ignoring clicks on the children a tags within it. You can check out my attempt (which is not working as expected) on this JSFiddle link: http://jsfiddle.net/q15s25Lx/ ...

The response error from the callback of the HTTP service in CouchDB with AngularJS lacks any meaningful data

I need help writing CouchDB TypeScript classes with AngularJS http service. My call is successful in the happy path, but I'm struggling to retrieve the status code or JSON error information from the server, which is crucial for user feedback. public ...

What is the best way to store textarea information into a session using the jQuery AJAX post method in Codeigniter?

I am having trouble saving the text from a textarea to the Controller session using jQuery AJAX post method in Codeigniter Smarty. I am unable to save the data to the session. Can someone please provide me with guidance and a detailed example of how to ach ...

Js: Automatically populating data into various input fields

I've encountered an issue with form input value injection using a <script> and POST requests. When I attempt to create another form with the same input field (same name and id), the value injection doesn't work, and troubleshooting has bee ...

Is it possible to use vanilla JavaScript scroll event with AngularJS framework?

I am attempting to track the window offset from the top of the document, but I am facing issues with jQuery scroll functionality. Can a vanilla JavaScript scroll event listener be used effectively within an Angular environment? app.directive('owlCaro ...