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:561
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 { filterArray } from "@evolu/common";
const evens = filterArray([1, 2, 3, 4, 5], (x) => x % 2 === 0);
expect(evens).toEqual([2, 4]);
With refinement
import { filterArray, NonEmptyTrimmedString, PositiveInt } from "@evolu/common";
const mixed: ReadonlyArray<NonEmptyTrimmedString | PositiveInt> = [
NonEmptyTrimmedString.orThrow("hello"),
PositiveInt.orThrow(42),
];
const positiveInts = filterArray(mixed, PositiveInt.is);
expect(positiveInts).toEqual([42]);
expectTypeOf(positiveInts).toEqualTypeOf<ReadonlyArray<PositiveInt>>();
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:566
With predicate.