小编Pao*_*der的帖子

Typescript 中通过方法名称获取类方法的返回类型

假设我们有一堂课:

class Foo {
  var1: string = 'var1';
  var2: string = 'var2';

  hello(request: A): Promise<B> {  }

  world(request: C): Promise<D> {  }
}
Run Code Online (Sandbox Code Playgroud)

我想实现执行以下实例方法的函数Foo

const foo = new Foo();
const executeFoo = (methodName: string, firstParam: any) => { // <- I'm stuck in this arrow function.
  return foo[methodName](firstParam);
};

executeFoo('hello', testParam); // testParams is type of A, then return type should Promise<B>.
executeFoo('world', testParam2); // testParams2 is type of C, then return type should Promise<D>.
Run Code Online (Sandbox Code Playgroud)

有没有办法定义 的类型executeFoo …

javascript higher-order-functions typescript function-definition

0
推荐指数
1
解决办法
2295
查看次数