Skip to content

Commit 3e6f0ec

Browse files
committed
fix: fix up type definitions for R.flatMap and AR.flatMap
1 parent cb0eb2d commit 3e6f0ec

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

__tests__/Result/flatMap.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1+
import { expectType } from 'ts-expect'
12
import { pipe, R } from '../..'
23

4+
class Error1 extends Error {}
5+
class Error2 extends Error {}
6+
37
describe('flatMap', () => {
48
it('returns Error', () => {
9+
expectType<R.Result<string, Error1 | Error2>>(
10+
pipe(
11+
R.fromNullable('test', new Error1()),
12+
R.flatMap(value => {
13+
return Math.random() < 0.5 ? R.Ok(value) : R.Error(new Error2())
14+
}),
15+
),
16+
)
17+
expectType<R.Result<string, Error1>>(
18+
pipe(
19+
R.fromNullable('test', new Error1()),
20+
R.flatMap(value => {
21+
return Math.random() < 0.5 ? R.Ok(value) : R.Error(new Error1())
22+
}),
23+
),
24+
)
25+
526
expect(
627
pipe(
728
R.fromNullable('value', 'this is bad'),

src/AsyncResult/AsyncResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export declare function make<A, B = globalThis.Error>(
1010
export declare function flatMap<A, B, C, D = B>(
1111
promise: AsyncResult<A, B>,
1212
mapFn: (value: A) => AsyncResult<C, D>,
13-
): AsyncResult<C, D>
13+
): AsyncResult<C, B | D>
1414

1515
export declare function fold<A, B, C, D = B>(
1616
promise: AsyncResult<A, B>,

src/AsyncResult/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export declare function map<A, B, C>(
2323
export declare function flatMap<A, B, C, D = B>(
2424
promise: AsyncResult<A, B>,
2525
mapFn: (value: A) => AsyncResult<C, D>,
26-
): AsyncResult<C, D>
26+
): AsyncResult<C, B | D>
2727
export declare function flatMap<A, B, C, D = B>(
2828
mapFn: (value: A) => AsyncResult<C, D>,
29-
): (promise: AsyncResult<A, B>) => AsyncResult<C, D>
29+
): (promise: AsyncResult<A, B>) => AsyncResult<C, B | D>
3030
export declare function fold<A, B, C, D = B>(
3131
promise: AsyncResult<A, B>,
3232
mapFn: (value: A) => Result<C, D>,

src/Result/Result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export declare function fromPromise<A>(
4646
export declare function flatMap<A, B, C, D = B>(
4747
result: Result<A, B>,
4848
mapFn: (value: A) => Result<C, D>,
49-
): Result<C, D>
49+
): Result<C, B | D>
5050
export declare function map<A, B, C>(
5151
result: Result<A, B>,
5252
mapFn: (value: A) => C,

src/Result/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ export declare function mapWithDefault<A, B, C>(
9999
export declare function flatMap<A, B, C, D = B>(
100100
result: Result<A, B>,
101101
mapFn: (value: A) => Result<C, D>,
102-
): Result<C, D>
102+
): Result<C, B | D>
103103

104104
export declare function flatMap<A, B, C, D = B>(
105105
mapFn: (value: A) => Result<C, D>,
106-
): (result: Result<A, B>) => Result<C, D>
106+
): (result: Result<A, B>) => Result<C, B | D>
107107

108108
/** Returns `value` if `result` is `Ok(value)`, otherwise, throws an exception. */
109109

0 commit comments

Comments
 (0)