Costa.
Costa.
TTCTheo's Typesafe Cult
Created by Costa. on 4/10/2023 in #questions
Use function as a parameter and preserve types
Hey guys, i'm trying to figure out a way to do the following I have and interface with some functions/promises
interface SomeInterface {
foo(...args): Promise<void>,
bar(...args): void
...
}
interface SomeInterface {
foo(...args): Promise<void>,
bar(...args): void
...
}
Is there a way the create a function wrapper with the parameter is a function of SomeInterface and the return type is the parameter of this function. Something like this:
function wrapper(func: FuncOfSomeInterface): ResultOfFunc {
...do smth here

const result = func(...args)

...do smth here

return result
}
function wrapper(func: FuncOfSomeInterface): ResultOfFunc {
...do smth here

const result = func(...args)

...do smth here

return result
}
Then i would do this: wrapper(foo(...args)) ✅ wrapper(wrong(...args)) ❌ I still a noob in typescript, so i'd be happy if someone help me ❤️
32 replies