API reference / @evolu/common / Array / filterArray

Function: filterArray()

Defined in: packages/common/src/Array.ts:237

Filters an array using a predicate or refinement function, returning a new readonly array.

Accepts both mutable and readonly arrays. 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.

Examples

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)

Type Parameters

Type Parameter
T
S

Parameters

ParameterType
arrayreadonly T[]
refinementRefinementWithIndex<T, S>

Returns

readonly S[]

Defined in: packages/common/src/Array.ts:241

Filters an array using a predicate or refinement function, returning a new readonly array.

Accepts both mutable and readonly arrays. 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.

Examples

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)

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayreadonly T[]
predicatePredicateWithIndex<T>

Returns

readonly T[]

Was this page helpful?