Reputation: 1
Before Typescript 2 version I used to convert enums to string in the following way:
public myFunction(myEnum: MyEnum): string {
return MyEnum[myEnum];
}
Now with the new version of Typescript the following error is generated: An index expression argument must be of type 'string', 'number', 'symbol' or 'any'.
Do you have an idea how I could fix this?
Upvotes: 0
Views: 669
Reputation: 250942
I believe the error you are getting is that you declare that myFunction
returns a string, but it actually doesn't return anything...
public myFunction(myEnum: MyEnum): string {
console.log(MyEnum[myEnum]);
}
Fix it by returning the value...
public myFunction(myEnum: MyEnum): string {
return MyEnum[myEnum];
}
Or by changing the return type...
public myFunction(myEnum: MyEnum): void{
console.log(MyEnum[myEnum]);
}
Full example to show things do still work.
enum Example {
Red,
Blue,
Green
}
alert(Example[Example.Red]);
function myFunction(myEnum: Example): string {
return(Example[myEnum]);
}
alert(myFunction(Example.Blue));
Upvotes: 1