variable / @typed/guard

filter

Keeps matched values that satisfy a predicate or refinement.

Package version
1.0.0-beta.13
Category
Input selection
Since
1.0.0

Import

import { filter } from "@typed/guard";

Signatures

export declare const filter: {
    <O, O2 extends O>(predicate: (o: O) => o is O2): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, O2, E, R>;
    <O>(predicate: (o: O) => boolean): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, R>;
    <I, O, E, R, O2 extends O>(guard: GuardInput<I, O, E, R>, predicate: (o: O) => o is O2): Guard<I, O2, E, R>;
    <I, O, E, R>(guard: GuardInput<I, O, E, R>, predicate: (o: O) => boolean): Guard<I, O, E, R>;
};

Why

Predicate failure remains successful None, preserving Guard dispatch semantics and type refinement.

Ownership and lifetime

This combinator acquires no resources; the predicate runs only for source matches.

Examples

import { filter, liftPredicate } from "@typed/guard"
const positive = filter(liftPredicate((u: unknown): u is number => typeof u === "number"), (n) => n > 0)

Source