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 help12 Replies
Can you send your current work function that you attempted?
it was something about that
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.
i have a problem with typescript types not the function implementation itself :(
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?
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
first callback can be just empty, i just want to have types on previous callbacks results in arguments of the next callbacks
of all of the previous callbacks or just the direct predecessor
all of previous would be nice
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
okay let me see
@choco would an api like
apply(cb1)(cb2)(cb3)
also work?absolutely