API reference › @evolu/common › Array › filterArray
Call Signature
function filterArray<T, S>(
array: readonly T[],
refinement: (item: T, index: number, array: readonly T[]) => item is S,
): readonly S[];
Defined in: packages/common/src/Array.ts:639
Filters an array using a predicate or refinement function, returning a new readonly array.
When used with a refinement function (with value is Type syntax),
TypeScript will narrow the result type to the narrowed type, making it useful
for filtering with Evolu Types like PositiveInt.is.
With predicate
import { assertEqual, filterArray } from "@evolu/common";
const evens = filterArray([1, 2, 3, 4, 5], (x) => x % 2 === 0);
assertEqual(evens, [2, 4]);
With refinement
import {
assertEqual,
assertType,
filterArray,
NonEmptyTrimmedString,
PositiveInt,
} from "@evolu/common";
const mixed: ReadonlyArray<NonEmptyTrimmedString | PositiveInt> = [
NonEmptyTrimmedString.orThrow("hello"),
PositiveInt.orThrow(42),
];
const positiveInts = filterArray(mixed, PositiveInt.is);
assertEqual(positiveInts, [42]);
assertType<ReadonlyArray<PositiveInt>, typeof positiveInts>();
The predicate receives (item, index, array), matching native
Array.filter.
Call Signature
function filterArray<T>(
array: readonly T[],
predicate: (item: T, index: number, array: readonly T[]) => boolean,
): readonly T[];
Defined in: packages/common/src/Array.ts:644
With predicate.