fp-ts-std icon indicating copy to clipboard operation
fp-ts-std copied to clipboard

`A.reject` loses type

Open OliverJAsh opened this issue 3 years ago • 3 comments

declare const xs: Array<string>;

pipe(
  xs,
  A.reject((value) => {
    value
    // ^?
    return true;
  }),
);

The type of value is unknown but it should be string.

I can fix it with the following patch:

-export declare const reject: <A>(f: Predicate<A>) => <B extends A>(xs: B[]) => B[];
+export declare const reject: <A>(f: Predicate<A>) => (xs: A[]) => A[];

However I suspect this causes other issues (https://github.com/samhh/fp-ts-std/issues/122).

FWIW A.filter has these overloads:

export declare const filter: {
  <A, B extends A>(refinement: Refinement<A, B>): (as: Array<A>) => Array<B>
  <A>(predicate: Predicate<A>): <B extends A>(bs: Array<B>) => Array<B>
  <A>(predicate: Predicate<A>): (as: Array<A>) => Array<A>
}

OliverJAsh avatar Nov 23 '22 19:11 OliverJAsh

Without the first overload, inferrence fails. I don't understand why.

Without the second overload, that subtyping behaviour regresses.

We can possibly drop the third overload though, redundant in the presence of the second one.


I guess the same will need replicating to some other functions that support subtyping as per #122.

samhh avatar Nov 24 '22 18:11 samhh

We can possibly drop the third overload though, redundant in the presence of the second one.

I think I found the same for O.fromPredicate: https://github.com/gcanti/fp-ts/issues/1808

OliverJAsh avatar Nov 24 '22 19:11 OliverJAsh

I don't know how to solve this considering that reject can't support refinements. No other combination of overloads or playing with type arguments seems to work. :thinking:

samhh avatar Apr 07 '23 15:04 samhh