Jameel Moideen
Jameel Moideen

Reputation: 7931

'===' cannot be applied to types 'string' and 'enum' string value

I have created enum string in Typescript like below

 export enum Widget {
    ICONWIDGET = "IconWidget",
};

But I am getting runtime error '===' cannot be applied to types 'string' and 'Widget' when I compare a string with enum string.

 getWidgetComponent(componentName:string) {

              if(componentName ===  Widget.ICONWIDGET){
                  return IconWidgetComponent;
              }
      }

I saw a similar kind of issue in reported in Github. Is there any workaround for this??

https://github.com/Microsoft/TypeScript/issues/11533

Upvotes: 1

Views: 1303

Answers (4)

Jameel Moideen
Jameel Moideen

Reputation: 7931

I Upgraded to latest version of Typescript(2.4.1) and it's working fine. The previous version of Typescript I used was 2.1.4.

npm install -g typescript@latest

Upvotes: 2

realbart
realbart

Reputation: 3984

You could simply assert:

getWidgetComponent(componentName:string) {

          if(componentName ===  (Widget.ICONWIDGET as string)){
              return IconWidgetComponent;
          }
  }

Upvotes: 0

SLaks
SLaks

Reputation: 887807

You can cast the enum value to string:

if(componentName === Widget.ICONWIDGET as string) {

Upvotes: 0

Related Questions