-
Notifications
You must be signed in to change notification settings - Fork 30
Refactor Result type
#14980
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
#14980
Conversation
A simple version of this type was introduced to see if we would find it
useful. We've started to use it more widely, so this change adds some
ergonomic improvements to make it easier to work with. There are three
parts to this, as detailed below.
This keeps the convenience functions `ok` and `error` for constructing
`Result`, but it's possible these are no longer necessary as the `Ok`
and `Err` classes could be constructed directly. An update to this may
be considered in a future change.
`kind` Becomes `ok`
-------------------
The `kind` property, which was previously of type `'ok' | 'error'`,
has been replaced with an `ok` property of type `boolean`. This makes
code that checks this property less verbose, and it's similar to the
`ok` property found on the `Response` type in the Fetch API.
**Before:**
```ts
if (result.kind === 'ok') {
console.log(result.value);
} else {
console.error(result.error);
}
```
**After:**
```ts
if (result.ok) {
console.log(result.value);
} else {
console.error(result.error);
}
```
New Methods
-----------
1. `map`
**Before:**
```ts
if (result.kind === 'error') {
return result;
}
return doSomething(result.value);
```
**After:**
```ts
return result.map(doSomething);
```
2. flatMap
**Before:**
```ts
if (result.kind === 'error') {
return result;
}
return doSomethingThatCouldFail(result.value);
```
**After:**
```ts
return result.flatMap(doSomethingThatCouldFail);
```
3. `mapError`
**Before:**
```ts
if (result.kind === 'error') {
return error(`New error message: ${result.error}`);
}
```
**After:**
```ts
return result.mapError(err => `New error message: ${err}`);
```
`okOrThrow` And `errorOrThrow` Become Methods
---------------------------------------------
Previously these were functions, but they have now become methods
alongside the others described above. They've also been renamed to
better suit the method approach.
**Before:**
```ts
const value = okOrThrow(doSomething(data), 'Expected this to succeed');
const error = errorOrThrow(
doSomething(data),
'Expected this to fail',
);
```
**After:**
```ts
const value = doSomething(data).getOrThrow('Expected this to succeed');
const error = doSomething(data).getErrorOrThrow(
'Expected this to fail',
);
```
|
I’m definitely partial to an |
|
Hello 👋! When you're ready to run Chromatic, please apply the You will need to reapply the label each time you want to run Chromatic. |
ioannakok
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 🥳
|
Rare @mxdvl sighting 💜 I like the new syntax, it feels a lot more like the syntax I'm used to with |
|
Seen on PROD (merged by @JamieB-gu 10 minutes and 8 seconds ago) Please check your changes! |
A simple version of this type was introduced to see if we would find it useful (#10361). We've started to use it more widely, so this change adds some ergonomic improvements to make it easier to work with. There are three parts to this, as detailed below.
This keeps the convenience functions
okanderrorfor constructingResult, but it's possible these are no longer necessary as theOkandErrclasses could be constructed directly. An update to this may be considered in a future change.Note: "Hide whitespace" may be useful for reviewing this change.
kindBecomesokThe
kindproperty, which was previously of type'ok' | 'error', has been replaced with anokproperty of typeboolean. This makes code that checks this property less verbose, and it's similar to theokproperty found on theResponsetype in the Fetch API1. I believe this was originally suggested by @mxdvlBefore:
After:
New Methods
mapBefore:
After:
flatMapBefore:
After:
mapErrorBefore:
After:
okOrThrowAnderrorOrThrowBecome MethodsPreviously these were functions, but they have now become methods alongside the others described above. They've also been renamed to better suit the method approach.
Before:
After:
Footnotes
https://developer.mozilla.org/en-US/docs/Web/API/Response/ok ↩