Simple typescript function

It has been several hours since I have been trying to create a simple function that I know is possible to create, because I once somehow managed to do that, but not now. I just want to create a function that takes other functions. ...callbacks Each of those functions will be called inside, and a single value will be passed (context). Context: It is an object that takes the result of the previous callback. execution. so like [0]: number, [1]: {a: number} The problem is that I cannot have a type of those results, but instead it displays as [x:string]:any. Again once i somehow implemented it and now i just cant. Thank you for any help
12 Replies
Iker
Iker2mo ago
Can you send your current work function that you attempted?
choco
chocoOP2mo ago
it was something about that
function attemp<T extends any[]>(...callbacks: ((context: {
results: {[K in keyof T as K extends `${infer N extends number}`
? N
: never]: T[K]}
}) => T[number])[]) {
const context = {
results: {}
};
callbacks.map((callback, index)=>{
const result = callback(context);
context.results = {...context.results, [index]: result};
})
}
function attemp<T extends any[]>(...callbacks: ((context: {
results: {[K in keyof T as K extends `${infer N extends number}`
? N
: never]: T[K]}
}) => T[number])[]) {
const context = {
results: {}
};
callbacks.map((callback, index)=>{
const result = callback(context);
context.results = {...context.results, [index]: result};
})
}
Kareem
Kareem2mo ago
Hi You can read about partial applications or monads from here, I think these solvrr your problem https://github.com/RealKareemAnees/GoFunctionalProgramming?tab=readme-ov-file#partial-application The code us written in Go but it is very easy to understand and you can use gpt to translate it
GitHub
GitHub - RealKareemAnees/GoFunctionalProgramming
Contribute to RealKareemAnees/GoFunctionalProgramming development by creating an account on GitHub.
choco
chocoOP2mo ago
i have a problem with typescript types not the function implementation itself :(
erik.gh
erik.gh2mo ago
okay so given the first callback returns a string the next callback's first parameter should be of type string correct? what about the type of the first callback's first parameter?
Kareem
Kareem2mo ago
Ah okay, one possible solution is to create a type for this function and spread it, furthermore I don't really know how to help. If you give like stepped approach on what you want to J can help
choco
chocoOP2mo ago
first callback can be just empty, i just want to have types on previous callbacks results in arguments of the next callbacks
erik.gh
erik.gh2mo ago
of all of the previous callbacks or just the direct predecessor
choco
chocoOP2mo ago
all of previous would be nice
Kareem
Kareem2mo ago
Use extended types of make an array type of fisrt empty element then a spread of filled types Like [cb_type_0, ...cb_type_rest] and see if this works Or just make 3 arguments, one for the context, one for the first cb and spread for the rest cps
erik.gh
erik.gh2mo ago
okay let me see @choco would an api like apply(cb1)(cb2)(cb3) also work?
choco
chocoOP2mo ago
absolutely

Did you find this page helpful?