Reputation: 7931
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
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
Reputation: 3984
This works:
enum Widget {
ICONWIDGET = "IconWidget"
};
class X {
getWidgetComponent(componentName: string) {
if (componentName === (Widget.ICONWIDGET as string)) {
return 'Put what you want to return here.';
}
}
}
var x = new X();
alert(x.getWidgetComponent("IconWidget"));
Upvotes: 0
Reputation: 3984
You could simply assert:
getWidgetComponent(componentName:string) {
if(componentName === (Widget.ICONWIDGET as string)){
return IconWidgetComponent;
}
}
Upvotes: 0
Reputation: 887807
You can cast the enum value to string:
if(componentName === Widget.ICONWIDGET as string) {
Upvotes: 0