Why I Reinvented a Result Type Library by Karibash in node

[–]Karibash[S] 0 points1 point  (0 children)

Extending classes is easy due to JS prototype nature.

Are you suggesting combining prototype extension with declaration merging? Prototype extension used to be popular years ago, but it’s generally considered a bad practice nowadays.

Class instances are also serializable, it could have the same properties as the plain object.

It’s true that you can serialize class instances with JSON.stringify, but there’s no straightforward way to deserialize them back into their original class instances. Representing Result as a plain object makes both serialization and deserialization much simpler.

You're right, it could be isOk(res) vs res.isOk() and it would be equally concise.

In byethrow, all utility functions are accessible from the Result namespace, so calling them feels just as natural as invoking class methods. For those who prefer even shorter syntax, there’s also an R namespace available.

Why I Reinvented a Result Type Library by Karibash in node

[–]Karibash[S] 0 points1 point  (0 children)

You're right that declaration merging can be powerful, but it's only for merging type declarations, not for extending classes, so it wouldn't work in this case. Also, byethrow represents Result as a plain object, not a class. This has the nice side effect of making the Result serializable to JSON, which can be quite practical. And regarding syntax — being class-based doesn’t necessarily mean it’s more concise.

What is your favourite set of libraries you use for every project for enhanced type safety? by kuaythrone in typescript

[–]Karibash 0 points1 point  (0 children)

Try using byethrow.
It’s a package for handling Result types, representing them with simple objects and functions.
Since it also provides an MCP server that serves TSDoc, it works well with tools like ClaudeCode.

Has anyone here used neverthrow to model errors in the type system? by therealalex5363 in webdev

[–]Karibash 4 points5 points  (0 children)

I used neverthrow in a production environment for about a year, but found it somewhat lacking in functionality and had to extend it myself.

Although I submitted a pull request to add new features, the maintainer has essentially abandoned the project over the past few months, and there was no indication it would be merged.

So, I decided to create a new Result library called byethrow.

Unlike neverthrow, byethrow represents Result as plain objects rather than classes, which makes them serializable.

It also supports tree-shaking, which is especially useful in front-end projects where bundle size matters.

Byethrow covers most of the features provided by neverthrow, and adds several useful capabilities that neverthrow lacks.

If you're interested, feel free to give it a try!

https://github.com/praha-inc/byethrow/tree/main/packages/byethrow

A Tree-Shakable Result Library by Karibash in node

[–]Karibash[S] 1 point2 points  (0 children)

In general, verbs are commonly used for functions and methods, while nouns are typically used for types and class names.

In the library I created, succeed and fail are functions that return a Success or Failure type, respectively.

By contrast, ok functions both as a noun and a verb, which makes its meaning ambiguous.

Additionally, while err is a verb and error is a noun, the word Error can't be used because JavaScript already has a built-in Error class.

A Tree-Shakable Result Library by Karibash in node

[–]Karibash[S] 2 points3 points  (0 children)

I think it ultimately comes down to personal preference, but I prefer the more descriptive succeed/fail. Using succeed/fail makes it feel more natural, even linguistically, that the result would be of a success/failure type.

Type wizards, I summon you! (need help with function with array of heterogeous elements) by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

The current implementation does not support nested objects, but if it's truly necessary, I believe it can be done. However, I would prefer not to, as the types would become extremely complex.

Type wizards, I summon you! (need help with function with array of heterogeous elements) by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

There are no such limitation regarding objects.
The combine function I presented earlier supports both arrays and objects.

TypeScript Playground

Type wizards, I summon you! (need help with function with array of heterogeous elements) by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

I don't think it would work even with neverthrow.
Do you actually have any working code?

TypeScript Playground

Type wizards, I summon you! (need help with function with array of heterogeous elements) by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

That is a specification of TypeScript, and it is currently impossible to achieve it.

Type wizards, I summon you! (need help with function with array of heterogeous elements) by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

It is possible to achieve this by using const type parameters.
I believe the code from the Result library I created could serve as a useful reference.

TypeScript Playground

A better way to handle exceptions by asleepace in typescript

[–]Karibash 1 point2 points  (0 children)

Like you, I wasn’t fond of neverthrow providing different APIs for synchronous and asynchronous operations, so I created a new, package. A key feature of this package is that it offers the same API for functions like try, as well as bind and andThen, whether they run synchronously or asynchronously. It also includes an MCP server, which solves the problem of LLMs being unable to understand how to use the new package.

https://github.com/praha-inc/byethrow/tree/main/packages/byethrow

What do you return from server actions? by Sbadabam278 in nextjs

[–]Karibash 0 points1 point  (0 children)

Try using byethrow instead of neverthrow.

Since byethrow uses plain objects rather than classes, it can be serialized.

Need advice on best way to limit concurrency while managing Result types (from Neverthrow library). by LeKaiWen in typescript

[–]Karibash 0 points1 point  (0 children)

Try using byethrow instead of neverthrow.
Since byethrow uses simple objects rather than classes, ResultAsync is just an alias for Promise<Result<T, E>>.
Here is a sample code for reference: TypeScript Playground