mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

Add type guard compatible overload for `O.filter` #77

Open lieberkind opened 1 year ago

lieberkind commented 1 year ago

The O.filter function doesn't have an overload that takes type guards into account. The only definition that exists looks like this:

export declare function filter<A>(predicateFn: (value: A) => boolean): (option: Option<A>) => Option<A>;

Compare this to A.filter which has an overload that allows for a type guard to be passed in:

export declare function filter<A, B extends A>(predicateFn: (value: A) => value is B): (xs: Array<A>) => Array<B>;

It would be nice to have the same for O.filter.

Here's an example that currently fails:

type WithOrWithoutYou = {type: 'with', you: string} | {type: 'without'};

declare const withOrWithout: WithOrWithoutYou;

pipe(
  O.some(withOrWithout),
  O.filter(w => w.type === 'with'),
  O.map(w => w.you), // <--- Fails with: Property 'you' does not exist on type 'WithOrWithoutYou'.
)

Thanks for a great library! 🙌

JUSTIVE commented 5 months ago

Hi. I made a PR to resolve this issue. please have a look and let me know if it's right.