Multiple Type Arguments in Generic Functions
Here we have another safeFunction
, but we are now passing in arguments:
type PromiseFunc<TResult> = (...args: any[]) => Promise<TResult>;const safeFunction = <TResult>(func: PromiseFunc<TResult>) => async (...args: any[]) => { try { const result = await func(...args);
Transcript
00:00 In this exercise we have a safe function, which is almost the same as before, except for the fact we're passing in some arguments now. So our safe function, or the thing that we pass into safe function, can receive arguments. So this means that the function that we get back should also contain those arguments and require you to pass them in.
00:19 But currently, the type is a little bit too wide, so this function that we're getting back isn't inferring name string here, it's actually saying that we can pass in as many arguments as we want to into the function, which isn't correct. The result is still being inferred correctly, still getting promise string or error, but yeah, not quite right.
00:39 So, we need to add a second type parameter onto promise func and onto safe function to actually infer the type of these arguments and make it work. And this means then that we'll be able to handle the case where, first of all, this function here, where there's no arguments or no parameters that are required,
00:57 and then in this one where there's one parameter that's required. So that's your job, to figure out how to change the types of the function and the generic type in order to make it work.