Reputation: 5720
I have a typescript class with a series of static methods
class MyClass{
static Method01(){}
static Method02(){}
...
static Method0N(){}
}
In some other class I get a parameter "action" and invoke:
try{
MyClass[action]()
}catch(error){
console.log(error)
}
Linter warns me by saying:
Element implicitly has an 'any' type because type 'typeof MyClass' has no index signature.ts
How can I skip this warning?
Upvotes: 1
Views: 29
Reputation: 1971
You should somehow narrow your action
variable to a string literal (static method name). How to achieve this depends on your use case.
const action = 'Method01';
// Or
let action = 'Method02' as const;
// Or
type Action = keyof typeof MyClass;
let action: Action = 'Method0N';
MyClass[action] // no warning
Upvotes: 1