My call
method has two signatures and a single implementation:
call<T extends CallChannel, TArgs extends CallParameters[T]>(channel: T, ...args: TArgs): ReturnType<CallListener<T>>;
call<T extends SharedChannel, TArgs extends SharedParameters[T]>(channel: T, ...args: TArgs): ReturnType<SharedListener<T>>;
call(channel, ...args) { ... }
The issue is that Typescript is not typechecking properly because the arguments for call
are automatically typed as any
. These arguments should actually be either of type CallChannel
or SharedChannel
for the first argument, and one of two specific types for the second argument.
The ideal implementation would look like this:
call<T extends CallChannel | SharedChannel, TArgs extends CallParameters[T] | SharedParameters[T]> (channel: T, ...args: TArgs) {
Unfortunately, this doesn't work because the type CallParameters[T]
cannot resolve when T
is a SharedChannel
(and vice versa).
I have already provided all necessary type information to define call
, so how can I satisfy Typescript with the implementation signature?