Skip to content

Commit

Permalink
fix: fix up type definitions for R.flatMap and AR.flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Jan 4, 2023
1 parent cb0eb2d commit 3e6f0ec
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
21 changes: 21 additions & 0 deletions __tests__/Result/flatMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { expectType } from 'ts-expect'
import { pipe, R } from '../..'

class Error1 extends Error {}
class Error2 extends Error {}

describe('flatMap', () => {
it('returns Error', () => {
expectType<R.Result<string, Error1 | Error2>>(
pipe(
R.fromNullable('test', new Error1()),
R.flatMap(value => {
return Math.random() < 0.5 ? R.Ok(value) : R.Error(new Error2())
}),
),
)
expectType<R.Result<string, Error1>>(
pipe(
R.fromNullable('test', new Error1()),
R.flatMap(value => {
return Math.random() < 0.5 ? R.Ok(value) : R.Error(new Error1())
}),
),
)

expect(
pipe(
R.fromNullable('value', 'this is bad'),
Expand Down
2 changes: 1 addition & 1 deletion src/AsyncResult/AsyncResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export declare function make<A, B = globalThis.Error>(
export declare function flatMap<A, B, C, D = B>(
promise: AsyncResult<A, B>,
mapFn: (value: A) => AsyncResult<C, D>,
): AsyncResult<C, D>
): AsyncResult<C, B | D>

export declare function fold<A, B, C, D = B>(
promise: AsyncResult<A, B>,
Expand Down
4 changes: 2 additions & 2 deletions src/AsyncResult/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export declare function map<A, B, C>(
export declare function flatMap<A, B, C, D = B>(
promise: AsyncResult<A, B>,
mapFn: (value: A) => AsyncResult<C, D>,
): AsyncResult<C, D>
): AsyncResult<C, B | D>
export declare function flatMap<A, B, C, D = B>(
mapFn: (value: A) => AsyncResult<C, D>,
): (promise: AsyncResult<A, B>) => AsyncResult<C, D>
): (promise: AsyncResult<A, B>) => AsyncResult<C, B | D>
export declare function fold<A, B, C, D = B>(
promise: AsyncResult<A, B>,
mapFn: (value: A) => Result<C, D>,
Expand Down
2 changes: 1 addition & 1 deletion src/Result/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export declare function fromPromise<A>(
export declare function flatMap<A, B, C, D = B>(
result: Result<A, B>,
mapFn: (value: A) => Result<C, D>,
): Result<C, D>
): Result<C, B | D>
export declare function map<A, B, C>(
result: Result<A, B>,
mapFn: (value: A) => C,
Expand Down
4 changes: 2 additions & 2 deletions src/Result/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export declare function mapWithDefault<A, B, C>(
export declare function flatMap<A, B, C, D = B>(
result: Result<A, B>,
mapFn: (value: A) => Result<C, D>,
): Result<C, D>
): Result<C, B | D>

export declare function flatMap<A, B, C, D = B>(
mapFn: (value: A) => Result<C, D>,
): (result: Result<A, B>) => Result<C, D>
): (result: Result<A, B>) => Result<C, B | D>

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

Expand Down

0 comments on commit 3e6f0ec

Please sign in to comment.