API reference › @evolu/common › Array › filterArray
Call Signature
function filterArray<T, S>(
array: readonly T[],
refinement: RefinementWithIndex<T, S>,
): readonly S[];
Defined in: packages/common/src/Array.ts:465
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.
Example
With predicate
filterArray([1, 2, 3, 4, 5], (x) => x % 2 === 0); // [2, 4]
With refinement
const mixed: ReadonlyArray<NonEmptyString | PositiveInt> = [
NonEmptyString.orThrow("hello"),
PositiveInt.orThrow(42),
];
const positiveInts = filterArray(mixed, PositiveInt.is);
// positiveInts: ReadonlyArray<PositiveInt> (narrowed type)
Call Signature
function filterArray<T>(
array: readonly T[],
predicate: PredicateWithIndex<T>,
): readonly T[];
Defined in: packages/common/src/Array.ts:470
With predicate.