Tips for effectively utilizing Enums with switch statements in typescript

In one file, I have the following code snippet:

export module  Utils {
    export enum DataSources {
        SharepointList = "SharepointList",
        JsonData = "JsonData"
    };
}

And in another file, there is the following code:

import CustomerDAO from "./ICustomerDAO";
import SharepointListDAOFactory from "./SharepointListDAOFactory";
import JsonDAOFactory from "./JsonDAOFactory";
import {Utils} from "./DatasourcesEnum";


export default abstract class DAOFactory{

    public static SHAREPOINTLIST: number = 1;
    public static REMOTEJSON : number = 2;

    public abstract getCustomerDAO(): CustomerDAO;

    public  static getDAOFactory(whichFactory: Utils.DataSources): DAOFactory {   
      switch (whichFactory) {
        case whichFactory.SharepointList: 
            return new SharepointListDAOFactory();
        case whichFactory.JsonData: 
            return new JsonDAOFactory();      
        default  : 
            return null;
      }
    }
}

Unfortunately, these errors are appearing:

Property 'SharepointList' does not exist on type 'DataSources'.

Answer №1

It appears that the error you're encountering may be due to a mistake made with the enum usage in your code. Specifically, it seems like you're utilizing the assigned value instead of the actual enum itself within your switch statement.

public  static getDAOFactory(whichFactory: Utils.DataSources): DAOFactory {   
   switch (whichFactory) {
     case Utils.DataSources.SharepointList: 
       return new SharepointListDAOFactory();
     case Utils.DataSources.JsonData: 
       return new JsonDAOFactory();      
     default : 
       return null;
   }
 }

The whichFactory identifier should hold a value corresponding to Utils.DataSources, and it should be compared directly to the enum itself. The identifier will not encapsulate any other datasources enum values within it.

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

Modify the values of several dropdown menus (5 in total) at once using jQuery. The values will be dynamically retrieved from a JSON file

https://i.sstatic.net/9vjlz.png Dropdown values for the first dropdown are 1, 2, 3, 4, 5. Dropdown values for the second dropdown are 25, 26, 27, 28, 29. Dropdown values for the third dropdown are 35, 36, 37, 38, 39. The goal is to have each dropdown load ...

Steps for generating a Loader while exporting data from a dataTable

I'm attempting to hide a div with the `dataTable` and display a div with a `loader` while exporting data from the dataTable using one of the buttons, but nothing seems to be happening. DataTableButtons version 1.6.2 Here's my code: HTM ...

Ways to apply CSS in jQuery using various combinators

I'm facing an issue with changing CSS using jQuery. The specific CSS line I want to modify is: .switch-vertical input ~ input:checked ~ .toggle-outside .toggle-inside { top: 40px; } My attempt at changing it looked like this: $('.switch-vertic ...

Acquiring the specific checkbox value using JQuery

I am encountering an issue with my JQuery function that is supposed to print out the unique value assigned to each checkbox when clicked. Instead of displaying the correct values, it only prints out '1'. Each checkbox is assigned an item.id based ...

Why isn't res.send() in Node.js sending the message?

Despite numerous attempts, I am unable to send my response message. It was working briefly, but after some code changes, it stopped functioning. Can anyone provide a solution to this challenging issue? exports.listBoth = function(req, res) { ...

Node.js Internal Server Error

Currently in the process of setting up a deployment on Heroku, everything runs smoothly locally but encountering an error on Heroku. at=info method=GET path="/" host=pure-cliffs-68507.herokuapp.com request_id=66cd6aa8-209d-4065-88ce-ad201ce6e713 fwd="210 ...

Creating a 3D cube with visual graphics as its faces using Three.js

I've been experimenting with different solutions but haven't had any luck finding a solution online. The error I'm encountering when running my program is: XMLHttpRequest cannot load file:///C:/Users/winst/Documents/Programming%20Projects/Mi ...

Struggling with textpath SVG elements in jQuery

Currently, I am implementing SVG in my project and aiming to apply the toggleClass function using jQuery on the textpath elements upon clicking. My initial plan was: $("text#names > textpath").click(function() { $(this).toggleClass("newClass"); }) ...

Is the treatment of an Observable distinct in Lambda compared to a Promise?

We have integrated the NestJS CQRS package into our application, which enables us to create 'sagas' through the generation of RxJS Observables that trigger various background tasks. An issue surfaced when deploying the application on AWS Lambda ...

Picture in the form of a radio button

Is there a way to use an image instead of a radio button in my AngularJS form? I have managed to hide the radio button using CSS, but it disables the checked event. How can I make the image clickable? position: absolute; left: -9999px; Here is the code s ...

What could be causing the lack of functionality for my button click in my JavaScript and HTML setup?

Currently, I am attempting to implement a functionality where I have two buttons at the top of my page. One button displays "French" by default, and when I click on the "English" button, it should replace the text with "French" using show and hide methods. ...

Next.js encountered a TypeError while attempting to parse the URL from a specific location when targeting the API route relatively

I'm encountering an issue with my code in the app/page (Server Component): const getUsers = async () => { const result = await fetch('/api/users', {method: 'GET'}); if (result.ok) { return result.json(); } return []; ...

Angular 14: Removing elements from FormArray causes the remaining elements to lose their values

My form includes custom inputs and utilizes the ControlValueAccessor. To view the implementation, visit this link. I encountered an issue where deleting an item from a FormArray would inadvertently clear the values of the other FormGroups still present in ...

Parsing JSON data received through a URL using Ruby

When attempting to send a JSON object from JavaScript to Ruby, I am encountering issues with parsing it in Ruby. Despite trying various methods and conducting extensive research, I have not been able to find a solution. It is important to note that I am r ...

Iterate through the object received from the node promise and pass it to the subsequent .then method

After grappling with this issue for what feels like an eternity, I find myself immersed in learning about Node.js and trying to grasp the concept of promises. My current challenge involves retrieving data from the Spotify API, starting with fetching my ow ...

Creating a custom PHP function to handle intervals in PHP

I am looking for a PHP function that has similar functionality to the setInterval() function in JavaScript. My attempt so far: setInterval(function() { <?php echo getAllRooms(); ?> }, 2000); ...

Issue with Typescript not recognizing default properties on components

Can someone help me troubleshoot the issue I'm encountering in this code snippet: export type PackageLanguage = "de" | "en"; export interface ICookieConsentProps { language?: PackageLanguage ; } function CookieConsent({ langua ...

Sort the currency column in an HTML table through JavaScript

I have a code snippet below that I'm currently utilizing to arrange columns in an HTML table. The code works perfectly for alphabetical sorting and also for single-digit numbers. However, when attempting to sort a column containing currency values, t ...

Utilize the Jest moduleNameMapper for locating files: "resolver": undefined

Among the various files I have, there is a text file located in the component directory within the path: src/components/text Despite this, Jest is unable to locate the file when utilizing the webpack alias import Text from "components/text"; I ...

Retrieve the value of a JavaScript variable within an HTML or EJS document

My JavaScript file, named main.js, contains a JSON object like the following: const person = {}; person.AGE = '17'; person.GENDER = 'Male'; To load this file in my .ejs file, I use the following code: <script type="text/javascript ...