typescriptindex-signature

TypeScript index signature with promises


What is the correct index signature for the following class?

class MyClass {
  [index: string]: Promise<void> | Promise<MyType>; // not working

  public async methodOne (): Promise<void> { ... }
  public async methodTwo (): Promise<MyType> { ... }
}

I want to be able to execute a method on this class using the string name of the method:

myClassInstance[stringNameOfMethodOne]()

There are two TypeScript errors, one on the method definition and one on the usage of the method. The method definition error is:

Property 'methodOne' of type '() => Promise<void>' is not assignable to 'string' index type 'Promise<void> | Promise<MyType>'

The error on the usage of the method is:

This expression is not callable. No constituent of type 'Promise<MyType> | Promise<void>' is callable.

I've done this in JavaScript but am less familiar with TypeScript's index signatures.


Solution

  • You probably just forgot the function type:

    class MyClass {
      [index: string]: () => (Promise<void> | Promise<MyType>); // A function type returning a Promise
    
      public async methodOne (): Promise<void> { ... }
      public async methodTwo (): Promise<MyType> { ... }
    }