我有以下一段代码:
class Gateway {}
class GatewayA extends Gateway { open() {} }
class GatewayB extends Gateway { }
type UseCase<T> = (...gateways: Gateway[]) => () => Promise<T>;
const gatewayA = new GatewayA;
const gatewayB = new GatewayB;
const fooUseCase: UseCase<boolean> = (gatewayA: GatewayA, gatewayB: GatewayB) => async () => {
console.log(gatewayA);
console.log(gatewayB);
return true;
};
fooUseCase(gatewayA, gatewayB);
Gateways继承自Gateway基类,可能会添加新方法.此外,用例可以有不同的网关参数,例如,这意味着fooUseCase
需要GatewayA
和GatewayB
,但barUseCase
可能需要GatewayC
.
但TS抛出了这个错误:
Type '(gatewayA: GatewayA, gatewayB: GatewayB) => () => Promise<true>' is not assignable to type 'UseCase<boolean>'.
Types of parameters 'gatewayA' and 'gateways' are incompatible.
Property 'open' is missing in type 'Gateway' but required in type 'GatewayA'.ts(2322)
在不知道网关的哪些实现将作为参数传递的情况下,我如何定义UseCase
类型?
谢谢.