I made a class doing like enum following: https://stackoverflow.com/a/51398471
export default class Juice
{
[key: string]: any;
static APPLE = new Juice('APPLE', 'Apple juice');
static ORANGE = new Juice('ORANGE', 'Orange juice');
private constructor(private key:string, public readonly text:string) {
};
}
When I get access with key I defined, it works fine, but it failed when I try to access by dynamically like this:
console.log(Juice.APPLE); //works fine
console.log(Juice['APPLE']); //works fine
const key = 'APPLE'; //works fine
console.log(Juice[key]); //works fine
console.log(Object.keys(Juice).map((key:string) => Juice[key])); // error!
The error is:
TypeScript error in `path`
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Juice'.
No index signature with a parameter of type 'string' was found on type 'typeof Juice'. TS7053
Is there someone help me what the cause of the error is and the solution?
Please help, thanks.
I added index signature in the class, but it didn't help
[key: string]: any;
export default class Juice
{
[key: string]: any;
static APPLE = new Juice('APPLE', 'Apple juice');
static ORANGE = new Juice('ORANGE', 'Orange juice');
private constructor(private key:string, public readonly text:string) {
};
}
Get the list of the enum
class.