Spreading Rest Arguments
I'm attempting to write a function which eventually calls another function with the initially provided arguments. I'm having trouble getting it to work, so any help would be much appreciated!
Playground: https://www.typescriptlang.org/play?#code/PTAEDECcHsFtQC4AsCWkAmBaADgQ0ggJ6gA2KARpPoQFwBQAxtAHYDOCosh4ArswwhQtWoALygA3nVCgAZmna9+glgAp8AcwCMNUO0gpmGgDShNAJl3MescgFNIASknSZiJDADuoZne8BRSBhIVQByADloDhRYbBI7WDtmBDt0UMdXAF9jV1Y7JmZ0JQEhZnVIbV1yFA1DBFMLKxt7SFAAHx8eEhIGioBmKuhoeNxmZyk3dy8fP1BA4LDI6Nj4xOTU9Ky6TLpGElxWEQAVO3YXGWwecjIGUDsAD3yeFOKVZgAeI4BpO2IHlMKIgA1r9oLJEIRsHYwZxuHwSsIAHyqEG0UDfX6mAB0OM0rF0AAV8LhEilIKx3kQoTCuK9SqwANoYwgAXUR41cMhAoAAItA9HA7HoEDxZOCcVjOaBIHYRZBmLC6cIGaiWaoJXiMjIdpkgA
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
9 Replies
looked at this and your issue is essentially that the "...args" is a union of tuples, but must be a single tuple
you must infer which one of the tuples it is somehow, probably with a switch statement
or some typescript wizardry 😄
The thing is, that the code, the auto-completion and type-hinting when using the function all works. It's really just a type error that I don't understand.
Parameters<typeof myFunctions[TKey]>
only returns the array of parameters needed for the required function. (e.g. type ParamsOfFirstFunction = Parameters<typeof myFunctions['firstFunction']>
only returns one array/tuple).personally I would rewrite it so that your method takes 1 instead of 2, like this
Hmm, let me see if that works when I make it generic.
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Unfortunately, the exact same error. And manually switching through all possible actions is not an option. The functions can change from time to time.
Does anyone else have an idea?
https://tsplay.dev/m0YeDW
does this do what you want?
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@Christoph
That's not quite right. When you call the function now, arg1 would be the array of arguments and arg2 would be undefined.