Skip to content

Commit

Permalink
feat: improve refinement narrowing in R.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorales committed Oct 14, 2023
1 parent 624d300 commit 1e7c71b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-timers-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'funkcia': minor
---

Improve predicates refinement on dissatisfied paths in `Result`
15 changes: 8 additions & 7 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const fromFalsy: FromFalsy = dual(
interface FromPredicate {
<E, O, U extends O>(
refinement: Refinement<O, U>,
onDissatisfied: (failure: O) => E,
onDissatisfied: (failure: Exclude<O, U>) => E,
): (value: O) => Result<E, U>;
<E, O>(
predicate: Predicate<O>,
Expand All @@ -89,7 +89,7 @@ interface FromPredicate {
<E, O, U extends O>(
value: O,
refinement: Refinement<O, U>,
onDissatisfied: (failure: O) => E,
onDissatisfied: (failure: Exclude<O, U>) => E,
): Result<E, U>;
<E, O>(
value: O,
Expand Down Expand Up @@ -208,19 +208,20 @@ export const flatten: <E, E2, O>(
// filtering
// -------------------------------------

// @ts-expect-error the compiler complains about the implementation, but the overloading works when invoking the function
export function filter<O, O2 extends O, E2>(
refinement: Refinement<O, O2>,
onDissatisfied: (value: O) => E2,
onDissatisfied: (value: Exclude<O, O2>) => E2,
): <E>(self: Result<E, O>) => Result<E | E2, O2>;
export function filter<O, E2>(
predicate: Predicate<O>,
onDissatisfied: (value: O) => E2,
): <E>(self: Result<E, O>) => Result<E | E2, O>;
export function filter(
predicate: Predicate<unknown>,
onDissatisfied: (value: unknown) => unknown,
): (self: Result<unknown, unknown>) => Result<unknown, unknown> {
return (self) => {
predicate: (value: any) => boolean,
onDissatisfied: (value: any) => void,
) {
return (self: Result<any, any>) => {
if (_.isError(self)) {
return self;
}
Expand Down

0 comments on commit 1e7c71b

Please sign in to comment.