在打字稿中将一种类型转换为另一种类型

时间:2020-01-26 15:20:16

标签: typescript types

我有一个接受类型A的函数。该函数调用另一个函数

export const myFunc = async (event: A): Promise<A> => controller.myFunc(event);

myFunc()具有装饰器@Decorator,可将A转换为B

export function Decorator(): any {

  return (target: any, propertyKey: string, descriptor?: TypedPropertyDescriptor<any>) => {
     ...
     return new B();
  }
}

并且myFunc接受类型B作为参数

@Decorator()
public myFunc (input: B): Promise<A> {
  ...
}

这给了我一个编译器错误,因为myFunc期望B,但是我从controller.myFunc传递了类型A。

有没有一种方法可以转换类型,以便编译器停止抱怨? 干杯

0 个答案:

没有答案