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

feat: add types for Ok/Err/Result/ResultPromise #132

Merged
merged 1 commit into from
Aug 15, 2024
Merged

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Jul 22, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Added Types

  • Ok
    • Ok<string>[undefined, string]
    • Ok<Promise<string>>[undefined, string]
  • Err
    • Err[Error, undefined]
    • Err<unknown>[NonNullable<unknown>, undefined]
    • Err<Error | string>[Error | string, undefined]
  • Result
    • Result<string>Ok<string> | Err
    • Result<string, TypeError>Ok<string> | Err<TypeError>
  • ResultPromise
    • ResultPromise<string>Promise<Ok<string> | Err>
    • ResultPromise<string, TypeError>Promise<Ok<string> | Err<TypeError>>

The goal is to keep these types as straight-forward as possible.

Etymology

The Ok<T> and Err<E> convention for representing success and error states in a type-safe manner originates from the Rust programming language. This pattern is known as the Result type in Rust.

In Rust, the Result<T, E> enum is used to return and propagate errors. It has two variants:

  1. Ok(T): Represents a successful result, containing a value of type T.
  2. Err(E): Represents an error, containing an error value of type E.

This pattern has been adopted by other languages and libraries due to its effectiveness in handling errors and optional values in a type-safe way. It's similar to the Either type in functional programming, where Left typically represents an error and Right represents a success value.

The Ok<T> and Err<E> convention offers several benefits:

  1. Explicit error handling: Forces developers to consider both success and error cases.
  2. Type safety: Provides compile-time checks for proper error handling.
  3. Composability: Allows for easy chaining of operations that may fail.
  4. Avoiding exceptions for control flow: Represents errors as values rather than using exceptions.

This approach to error handling and optional values has gained popularity in various programming paradigms due to its clarity and safety advantages over traditional error handling mechanisms.

Related issue, if any:

Inspired by #131

For any code change,

  • Related documentation has been updated, if needed

Does this PR introduce a breaking change?

No

@aleclarson aleclarson added the new feature This PR adds a new function or extends an existing one label Jul 23, 2024
@aleclarson aleclarson marked this pull request as ready for review August 13, 2024 22:26
@aleclarson aleclarson force-pushed the feat/ok-err-types branch 5 times, most recently from 6f506ba to 7599e18 Compare August 15, 2024 21:53
@aleclarson aleclarson merged commit f5db070 into main Aug 15, 2024
8 checks passed
@aleclarson aleclarson deleted the feat/ok-err-types branch August 15, 2024 22:03
Copy link

A new beta version 12.2.0-beta.6b76988 has been published to NPM. 🚀

To install:

The radashi@beta tag also includes this PR.

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants