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

Refactor Result type #683

Merged
merged 4 commits into from
May 24, 2024
Merged

Refactor Result type #683

merged 4 commits into from
May 24, 2024

Conversation

mmgoodnow
Copy link
Collaborator

The current Result type is okay, but it doesn't actually leverage Typescript's ability to catch bugs at build time. This PR does a few things:

  • Result is now a union type of OkResult | ErrResult
  • unwrapOrThrow(err?: Error) has been separated into two methods:
    • unwrap() this method ONLY exists on OkResult. In order to call it, you need to use isOk or isErr to do type narrowing so that TypeScript knows the method exists.
    • unwrapOrThrow(err: Error) This is a convenience method to throw an error if the result is an ErrResult. It is like the old version of unwrapOrThrow, except the argument is now required. This exists on BOTH OkResult and ErrResult.
    • I migrated all calls to unwrapOrThrow() with no arguments to use unwrap(), and kept calls of unwrapOrThrow(new Error('error message')) with one argument to use unwrapOrThrow().
  • unwrapErr() this method ONLY exists on ErrResult. In order to call it, you need to use isOk or isErr to do type narrowing so that TypeScript knows the method exists.
  • isOk and isErr are now type predicates which means they will do type narrowing. Once you call result.isOk(), typescript "grants you access" to calling the unwrap() method.
  • I added a first-class isOk function (not an instance method on either of the result classes) because TypeScript doesn't understand when you .filter(r => r.isOk()). It understands filter(isOk) though.

@mmgoodnow mmgoodnow changed the title Mg refactor result class Refactor result class May 12, 2024
@mmgoodnow mmgoodnow changed the title Refactor result class Refactor Result type May 12, 2024
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mapErr<R>(mapper: (u: U) => R): Result<T, R> {
return this as unknown as Result<T, R>;
mapErr(): OkResult<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the return type intentional? I'm not familiar with JS or TS internals so I can't comment much.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah - the idea behind mapErr is that it's a no-op if the result is an OkResult, but it will do the transformation supplied if the result is an ErrResult.

@mmgoodnow mmgoodnow merged commit 2145067 into master May 24, 2024
5 checks passed
@mmgoodnow mmgoodnow deleted the mg-refactor-result-class branch May 24, 2024 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants