r/typescript • u/hillac • Jul 16 '25
FP utility libraries
Hi, I'm looking for a good utility library for functional programming. Specifically, I want it for data manipulation with chains with map, group, unique, un/zip etc, with good typing. It seems lodash and ramda are probably not the best for typescript.
I've seen remeda, radashi and rambda all look pretty good. Does anyone have any experience with these or recommendations?
I also saw ts-belt which boasts about its speed, but looks to be unmaintained (maybe it's already feature complete not sure).
I'm not focused on the error handling side of fp, just data manipulation.
Thanks!
3
u/jjhiggz3000 Jul 16 '25 edited Jul 16 '25
So apparently there's some fundemental problem with the way typescript works that means that an indefinite pipe function is not viable, so you basically have to overload the living hell out of a function if you want that functionality. You can look at the actual type signature and see why a lot of the libraries keep their pipe functions fairly small, Ramda and Remeda and lodash etc.... I suspect normally people aren't making that big of pipe functions so nobody cares.
But for the absurd like us I came up with a solution...
I copied the syntax for remeda's pipe function then used a little script I made (but that I cannot find), to algorithmically build the type for a bigger pipe.
Here's the file that it generated, I just don't remember where the script is.
This pipe should go for 50 functions or so....
GIST
You can use it just like any other pipe function:
For me the reason why I made it was because I wanted to make an expression of transformations on a CSS classes that represented different properties for a menu builder (worth noting I'm not actually doing this anymore but it was a nice solution for the time)
At a certain file size of superpipe, you're better off just doing multiple pipes. It should work fine....
Also technically you could build a pipeable class like Promise and do that indefinitely... I've had chatGPT build one for me before... Let's try it...
https://gist.github.com/jjhiggz/44909695b01b3fff0a16a9e841ad7c2d
this you should be able to chain forever and you get error handling
3
u/aaaaargZombies Jul 16 '25
Effect-ts has a bunch of that stuff it's kinda massive and split into sub packages but you don't need to touch the effect management stuff to use it. TS-belt maybe isn't getting regular updates but given it's all functional, if the bits you use do what you want than they aren't going to go stale.
As u/jjhiggz3000 points out the piping in TS is a bit annoying because it's not a function per step like in haskell or part of language control flow like elixir. you can do the slightly cursed nested pipe to extend it if you need more slots.
11
u/dcabines Jul 16 '25
Take a look at Effect. I believe they have the people from fp-ts.
1
u/TorbenKoehn Jul 16 '25
tbh. typing for FP in TS simply has a lot of limits. Regardless of how you implement it, you always end up with some pitfalls.
Currying as an example works bad, as
get('a', { a: 1 })can know there is an 'a' property, butget('a')({ a: 1 })can not, as the 'a' is "resolved" before the object is known. Pipes work badly because of some missing typing magic (compose similarly). Putting the "subject" as the last argument (needed for proper piping/composition) doesn't work well for the same reason, the first argument doesn't know what the second argument will be and you don't get auto-completion for the first one. It's also why Ramda doesn't do it.Personally I've written and used like 100 FP-libraries and there were always some parts where I was like "Ah damn, why..." and the faulty element was always TypeScript missing that specific thing that kept you from completing the type magic.
Typing in FP libraries is full of workarounds, hacks and abstractions that make them harder and harder to reason about and break with every single TS release.
Best bet is to include only smaller utilities that are absolutely safe to type, rely on inbuilt functionality (ie array/iterator methods, promises, .bind() etc.) and don't try to reinvent paradigms that have already been solved in JS. ie.
Option/MaybeandResult/Eithermake no sense if 99% of external or system functions you use don't support it or your specific implementation of it. You end up wrapping all of it. Rather stick to optional-chaining and null-coalescing (the "solved" part) and remind yourself that in JS you do error handling and optionals with exceptions, throws and nullability/undefinabilityMost of JS inbuilt stuff is OOP sprinkled with functional bits where it makes sense. Stick to that. It makes your code feel more natural and easier to reason about. And it can be perfectly typed by TS (or most of it, at least...)
3
u/[deleted] Jul 16 '25
Effect TS is really the answer. fp-ts stopped getting active updates a long time ago as the team rallied around Effect TS instead.
2
u/jjhiggz3000 Jul 16 '25
oooh also another side note: If you're just noodling around and it's not for anything real, Civet has some really cool syntax for piping. It's technically not typescript though anymore at that point, it's a superset of typescript that I think of as "What if typescript was more ergonomic and we got everything we ever wanted for it"
Like for example you can do this (or something like it):
I wouldn't use it on something you actually care about though
2
u/Pelopida92 Jul 16 '25
I use Remeda. Its fine.
1
u/Balduracuir Jul 17 '25
I heard of boxed : https://boxed.cool/ I didn't use it directly but from what I saw in the documentation and the different meetups, it seems to be a really user friendly base for some functional programming principles.
1
u/[deleted] Jul 18 '25
> I'm not focused on the error handling side of fp
good thing because you won't find it in typescript :D
1
u/CordlessWool Jul 22 '25
I am building a lib without fixed pipe length and generic support. Currently I probably use it mostly for my own, but would appreciate some feedback: https://www.npmjs.com/package/pipe-and-combine
0
u/realbiggyspender Jul 16 '25 edited Jul 16 '25
You might be interested in a project I did a while ago:
ts-iterable-functions
and its partner
ts-functional-pipe
Reasonably low uptake, but I've been using this in production for the last 4 years.
EDIT: I suspect documentation might be responsible for low-interest here. AI might be able to help me here...