Skip to content

Add type guard compatible overload for O.filter #77

Open
@lieberkind

Description

@lieberkind

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! 🙌

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions