Hacker News new | ask | show | jobs
by vmfunction 667 days ago
> I do wish that Typescript would offer some tools to make it more ergonomic to write performant unifying code (I kind of despise conditional types

Maybe give https://gcanti.github.io/fp-ts/ a go?

2 comments

I use fp.ts quite a lot! I think it's _extremely unfortunate_ that the API docs don't include top-level examples for basically anything, though. So when people hit a problem that fp-ts or io-ts can solve, I have to basically write a disclaimer about the slight impenetrability before suggesting it.

I really think that TS itself should offer syntax more or less matching what that lib does at the type level, but this is a bit of a maximalist request.

effect-ts is nowadays the de facto successor of fp-ts and has better docs.

As for fp-ts all APIs have examples/tests that show their usage.

Thanks for the effect-ts suggestion! Looks pretty compelling...
IMO Zod is much easier to understand than io-ts, the counterpart to fp-ts: https://zod.dev/
It’s been a while since I looked deeply at them, but IIRC the primary difference is that io-ts returns Result types. It’s trivial to wrap that to produce a value or throw if that feels more comfortable.

It’s possible there are other aspects of the APIs that differ in meaningful ways, but last I checked virtually all of the libraries with similar functionality (and there are many) have roughly the same concepts until your schemas themselves get deeply complex.

Zod can also return result types: T.safeParse(x)

It’s not a very full featured Result, but you can wrap it with your own type.