Help me type this function

I have a function that takes in an input function and returns another function who's input is the same type as the function passed into it.
function Foo<T>(callback: (input?: T) => void){
return async function(input?: T) => {
//...
}
}
function Foo<T>(callback: (input?: T) => void){
return async function(input?: T) => {
//...
}
}
However, I want the return function type to either require or not require input based on if callback has a parameter or not. How would I do this?
4 Replies
benten
bentenOP3y ago
thanks homie Hmm, so this works with the spread args but not for explicitly declaring args. For example,
export function createLoader<LDR extends (ctx: Context, input: Record<string, string[]>) => any>(loader: LDR) {
function loaderSetup(input: Parameters<LDR>[1]) {
//...
}
return loaderSetup;
}
export function createLoader<LDR extends (ctx: Context, input: Record<string, string[]>) => any>(loader: LDR) {
function loaderSetup(input: Parameters<LDR>[1]) {
//...
}
return loaderSetup;
}
benten
bentenOP2y ago
Hey just wanted to say I finally got around to using this and it worked great. So thank you ❤️ @CaptainStarz
Want results from more Discord servers?
Add your server