typescript function generic parameter in spread argument

Viewed 258

I have the following function:

type SomeType<T> = Record<string, T>;
declare function test<T>(...args: SomeType<T>[]): T;

I would like to call it like that (a should be of type number|string):

const a = test({a: 3}, {b: "as"});

but that way I get an error: Type 'string' is not assignable to type 'number'. can I make this work without explicitly passing the generic argument number|string or using conditional types?

Playground

1 Answers

Variadic tuples might help:

declare function test<T extends any[]>(...args: [...T]): T;

const a = test({ a: 3 }, { b: "as" });
Related