最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to properly use Enums in typescript switch - Stack Overflow

programmeradmin6浏览0评论

I have this on one file:

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

and on another file I have this:

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;
      }
    }
}

But I get these errors:

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

I have this on one file:

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

and on another file I have this:

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;
      }
    }
}

But I get these errors:

Property 'SharepointList' does not exist on type 'DataSources'.
Share Improve this question asked Dec 20, 2017 at 22:39 Luis ValenciaLuis Valencia 34.1k99 gold badges311 silver badges532 bronze badges 3
  • which version of tsc you're using? – OJ Kwon Commented Dec 20, 2017 at 22:40
  • I have 2.2....................... – Luis Valencia Commented Dec 20, 2017 at 22:42
  • the feature you attempt to use is available from 2.4 on – smnbbrv Commented Dec 20, 2017 at 22:46
Add a ment  | 

1 Answer 1

Reset to default 6

I'm not quite sure what the error you're getting means. It seems like you've made a mistake with your enum though. You're using the assigned value and not the enum itself in 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 will have some value which is reflected in Utils.DataSources, and you'd want to pare the data to the enum itself. The value will not have the other datasources enum contained within itself.

发布评论

评论列表(0)

  1. 暂无评论