Skip to content

Commit 7b60c13

Browse files
fix: unwrap Result typed objects
1 parent 571a82f commit 7b60c13

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

README.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ type Result<T, E> = Ok<T> | Err<E>
8282
8383
The `Ok` type wraps a successful value.
8484
85+
**Example:**
86+
87+
```ts
88+
const result = new Ok(42)
89+
```
90+
8591
**Type Definition:**
8692

8793
```ts
@@ -92,16 +98,16 @@ declare class Ok<T> {
9298
}
9399
```
94100

101+
### `Err`
102+
103+
The `Err` type wraps an error value.
104+
95105
**Example:**
96106

97107
```ts
98-
const result = new Ok(42)
108+
const result = new Err('Something went wrong')
99109
```
100110

101-
### `Err`
102-
103-
The `Err` type wraps an error value.
104-
105111
**Type Definition:**
106112

107113
```ts
@@ -112,12 +118,6 @@ declare class Err<E> {
112118
}
113119
```
114120

115-
**Example:**
116-
117-
```ts
118-
const result = new Err('Something went wrong')
119-
```
120-
121121
### `ok`
122122

123123
Shorthand function to create an `Ok` result. Use it to wrap a successful value.
@@ -152,20 +152,21 @@ function trySafe<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>
152152

153153
### `unwrap`
154154

155-
Unwraps a `Result` and returns a tuple with the value and error: `{ value, error }`.
155+
Unwraps a `Result`, `Ok`, or `Err` value and returns the value or error in an object. If the result is an `Ok`, the object contains the value and an `undefined` error. If the result is an `Err`, the object contains an `undefined` value and the error.
156156

157-
**Type Definition:**
157+
**Example:**
158158

159159
```ts
160-
function unwrap<T>(result: Ok<T>): { value: T, error: undefined }
161-
function unwrap<E>(result: Err<E>): { value: undefined, error: E }
160+
const result = trySafe(() => JSON.parse('{"foo":"bar"}'))
161+
const { value, error } = unwrap(result)
162162
```
163163

164-
**Example:**
164+
**Type Definition:**
165165

166166
```ts
167-
const result = trySafe(() => JSON.parse('{"foo":"bar"}'))
168-
const { value, error } = unwrap(result)
167+
function unwrap<T>(result: Ok<T>): { value: T, error: undefined }
168+
function unwrap<E>(result: Err<E>): { value: undefined, error: E }
169+
function unwrap<T, E>(result: Result<T, E>): { value: T, error: undefined } | { value: undefined, error: E }
169170
```
170171

171172
## Examples

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function trySafe<T, E = unknown>(
4242

4343
export function unwrap<T>(result: Ok<T>): { value: T, error: undefined }
4444
export function unwrap<E>(result: Err<E>): { value: undefined, error: E }
45+
export function unwrap<T, E>(result: Result<T, E>): { value: T, error: undefined } | { value: undefined, error: E }
4546
export function unwrap<T, E>(
4647
result: Result<T, E>,
4748
): { value: T, error: undefined } | { value: undefined, error: E } {

test/index.test.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('result type tests', () => {
2929
})
3030

3131
it('handles failed synchronous operations', () => {
32-
const result = trySafe(() => {
32+
const result = trySafe<never, Error>(() => {
3333
throw new Error('test')
3434
})
3535
expect(result).toBeInstanceOf(Err)
3636
assertErr(result)
3737
expect(result.error).toBeInstanceOf(Error)
38-
expect((result as Err<Error>).error.message).toBe('test')
39-
expectTypeOf(result).toMatchTypeOf<Err< unknown>>()
38+
expect(result.error.message).toBe('test')
39+
expectTypeOf(result).toMatchTypeOf<Err<unknown>>()
4040
})
4141

4242
it('handles successful asynchronous operations', async () => {
@@ -48,11 +48,11 @@ describe('result type tests', () => {
4848
})
4949

5050
it('handles failed asynchronous operations', async () => {
51-
const result = await trySafe(Promise.reject(new Error('test')))
51+
const result = await trySafe<never, Error>(Promise.reject(new Error('test')))
5252
expect(result).toBeInstanceOf(Err)
5353
assertErr(result)
5454
expect(result.error).toBeInstanceOf(Error)
55-
expect((result as Err<Error>).error.message).toBe('test')
55+
expect(result.error.message).toBe('test')
5656
expectTypeOf(result).toMatchTypeOf<Err<unknown>>()
5757
})
5858

@@ -84,19 +84,26 @@ describe('result type tests', () => {
8484
})
8585

8686
it('unwraps an Ok result', () => {
87-
const result = ok(42)
87+
const result = ok(1)
8888
const unwrapped = unwrap(result)
89-
expect(unwrapped).toEqual({ value: 42, error: undefined })
89+
expect(unwrapped).toEqual({ value: 1, error: undefined })
9090
expectTypeOf(unwrapped).toMatchTypeOf<{ value: number, error: undefined }>()
9191
})
9292

9393
it('unwraps an Err result', () => {
94-
const error = new Error('test error')
94+
const error = new Error('test')
9595
const result = err(error)
9696
const unwrapped = unwrap(result)
9797
expect(unwrapped).toEqual({ value: undefined, error })
9898
expectTypeOf(unwrapped).toMatchTypeOf<{ value: undefined, error: Error }>()
9999
})
100+
101+
it('unwraps a result with type inference', () => {
102+
const result = trySafe(() => 1)
103+
const unwrapped = unwrap(result)
104+
expect(unwrapped).toEqual({ value: 1, error: undefined })
105+
expectTypeOf(unwrapped).toMatchTypeOf<{ value: unknown, error: unknown }>()
106+
})
100107
})
101108

102109
function assertOk<T, E>(result: Result<T, E>): asserts result is Ok<T> {

0 commit comments

Comments
 (0)