Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ok/err/okAsync/errAsync to accept zero arguments when returning void #584

Merged
merged 3 commits into from
Nov 9, 2024

Conversation

macksal
Copy link
Contributor

@macksal macksal commented Sep 19, 2024

When implementing a function returning a result, it's often that I need to construct an Ok<void> object. However, it's not so ergonomic to do this. ok must be passed an argument, even if you explicitly specify the void type. This leads to passing a "dummy" value:

return ok<void>(undefined);

Meanwhile, in Promise-land, it's perfectly acceptable to call Promise.resolve() with no arguments, which returns a Promise<void>.

Interestingly, TS allows a parameter to be omitted if it has type void. However, this does not work if the type is generic and has been expanded to void or a union containing void. See microsoft/TypeScript#29131

This PR adds additional overloads for ok, err, okAsync and errAsync that separate out the case where the argument type is void. This allows the following to work as expected:

return ok<void>();
return ok();

Copy link

changeset-bot bot commented Sep 19, 2024

🦋 Changeset detected

Latest commit: 7767eab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
neverthrow Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@petermakeswebsites
Copy link

Would really appreciate this being implemented!

@macksal
Copy link
Contributor Author

macksal commented Oct 21, 2024

Would really appreciate this being implemented!

Please help by providing a code review and hopefully we can get attention from the maintainers @supermacro @m-shaka. I'm using this in production and have considered forking, but this is already a fairly niche library and I hope we could maintain some kind of central community.


export function err<T = never, E extends string = string>(err: E): Err<T, E>
export function err<T = never, E = unknown>(err: E): Err<T, E>
export function err<T = never, E extends void = void>(err: void): Err<T, void>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really any valid use-case for void errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used one personally. Perhaps someone is using it as a generic "failure" while making a quick and dirty prototype.

I included the err overload for completeness, not for any specific use case.

I think it would be more surprising for the consumer if ok/err didn't work the same way. Do you see any downside to this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my humble opinion, this is a case of YAGNI.
It feels wrong to be able to return an error which is void.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2c...

I don't disagree with you @Papooch . I don't really see a use case for it either. Doesn't mean there isn't one.

However, I will say I think there's an overYAGNIation in libraries in general. So many times I've come across a feature I've wanted and it hasn't been implemented because of YAGNI. No human being can think of all the possible use cases, and in this case, it's not like adding that overload has any drawbacks.

At the end of the day though, I don't really mind either way - I'd just like to see the void Ok implemented! So whatever makes everyone else happy to expedite that process is fine by me! :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo say that symmetry + having lower friction with prototypes are good enough reasons to add it, and that there isn't really a cost to implementing it or maintaining it, so YAGNI doesn't really apply. If anything I'd guess that the lack of symmetry introduces an edge case that will make doing anything on both an Ok value and an Err value more convoluted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok for me.
It looks like that Rust's Err accepts Unit type

Copy link
Collaborator

@m-shaka m-shaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @macksal !
@supermacro will merge the PR if it's ok for him


export function err<T = never, E extends string = string>(err: E): Err<T, E>
export function err<T = never, E = unknown>(err: E): Err<T, E>
export function err<T = never, E extends void = void>(err: void): Err<T, void>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok for me.
It looks like that Rust's Err accepts Unit type

@supermacro supermacro merged commit fd35945 into supermacro:master Nov 9, 2024
8 checks passed
@macksal macksal deleted the macksal/ok-void-argument branch November 25, 2024 06:45
@macksal
Copy link
Contributor Author

macksal commented Nov 25, 2024

Thank you @supermacro @m-shaka for merging.

Would you consider reviewing another PR of mine, #588? It is adding a commonly requested feature including test coverage. I will appreciate any comment on whether this feature could be accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants