How to wrap generic functions?

Viewed 37

Assuming I've imported the following function from a thirdparty library:

function swap<T>({a,b}: {a: T, b: T}){
    return {a:b,b:a}
}

How would I wrap such a function without rewriting the anonymous arg type? If it were not generic, I would do it like this:

function swap ({a,b}:{a:number,b:number}){
    return {a:b,b:a};
}

function wrappedSwap (...args: Parameters<typeof swap>){
    console.log(args);
    return swap(...args)
}

But when it's generic, the type automatically is "unknown". Is there a way to specify which type to use? I imagined it was something like this:

function wrappedSwap<T>(...args: Parameters<(typeof swap)<T>>){
    console.log(args);
    return swap(...args);
}

But that doesn't work. Is it even possible?

0 Answers
Related