I want to define a type in typescript that can be passed an arbitrary number of generic types. This type should then define, that if applied to a rest parameter, that it allows for the function to receive parameters in a multiple of the supplied types. An example could look as follows:
function single(...args: HeterogeneousRest<number>) {...}
// works
single();
single(1);
single(1, 2, 1, 3, 4);
// does not work
single(1, "foobar")
function double(...args: HeterogeneousRest<number, string>) {...}
// works
double();
double(1, 'foobar');
double(1, 'foobar', 2, 'bazbar');
// does not work
double(1);
double(1, 'foobar', 2);
double('foobar', 2);
In case of the second function double it would be allowed to repeat in this order number and then string an arbitrary number of times, ranging from 0 to infinity. The number of parameters allow then is of course a multiple of 2.
If three generic types were defined of course it would be a multiple of three etc.
The type BTW does not have to have this signature. I'm not hung up on this if its not possible to define types with arbitrary number of type parameters. It would also suffice to use the type as:
HeterogeneousRest<[number, string, boolean]>
HeterogeneousRest<number, HeterogeneousRest<string, HeterogeneousRest<boolean>>>
...
Whatever works is fine, as long as the conditions are satisfied.
Before anyone blames me, because there is already an article out somewhere that exactly describes what I'm looking for, I spent two days googling this with different search queries and tried to build it myself but couldn't succeed. And the only things that would work where overloads but I wanted to solve this with meta programming instead as the concrete example may have a lot of parameters.
I hope my explanation makes sense. Thank you in advance.