I'm trying to create a generic function that takes in two parameters, the first of which is an array of functions. The second parameter’s type should be the union of the return types of the first parameter's elements (all of which are functions).
Here is what I've tried:
type Fn<R> = () => R;
const a: Fn<true> = () => true;
const b: Fn<"somen"> = () => "somen";
interface C<ABUnion, ABReturnUnion> {
fns: ABUnion[];
returns: ABReturnUnion;
}
function getX<R>(fns: Array<Fn<R>>, returns: R) {
type FnsUnion = typeof fns[number];
const c: C<FnsUnion, R> = {
fns,
returns,
};
return c;
}
getX([b, a], "true");
The language service underlines b
in the getX
call, and displays the following error:
Type '"somen"' is not assignable to type 'true'.
Anyone know of a solution? Thank you!