Skip to content

filter

The filter() method selects the elements of an array that satisfy a predicate and returns a new array. It can also serve as a type guard to refine the typing of the kept elements.

Interactive example

Syntax

Classic signature

typescript
// Type Guard
function filter<
	GenericInput extends readonly unknown[],
	GenericOutput extends GenericInput[number]
>(
	input: GenericInput,
predicate: (
	element: GenericInput[number],
	params: ArrayFilterParams<GenericInput>
) => element is GenericOutput
): GenericOutput[]

// Boolean
function filter<
	GenericInput extends readonly unknown[]
>(
	input: GenericInput,
predicate: (
	element: GenericInput[number],
	params: ArrayFilterParams<GenericInput>
) => boolean
): GenericInput[number][]

Curried signature

typescript
// Type Guard
function filter<
	GenericInput extends readonly unknown[],
	GenericOutput extends GenericInput[number]
>(
predicate: (
	element: GenericInput[number],
	params: ArrayFilterParams<GenericInput>
) => element is GenericOutput
): (input: GenericInput) => GenericOutput[]

// Boolean
function filter<
	GenericInput extends readonly unknown[]
>(
predicate: (
	element: GenericInput[number],
	params: ArrayFilterParams<GenericInput>
) => boolean
): (array: GenericInput) => GenericInput[number][]

Helper parameters

typescript
interface ArrayFilterParams<
	GenericInputArray extends readonly unknown[]
> {
	index: number;
	self: GenericInputArray;
}

Parameters

  • input: The array to filter.
  • predicate: Function applied to each element. It decides whether the element is kept and can act as a type guard.
  • params.index: Position of the current element in the original array.
  • params.self: The whole array (useful for comparing or inspecting a neighbor during filtering).

Return value

A new array containing only the elements for which the predicate returns true. The order is preserved and the initial array remains unchanged.

See also

  • map - Transforms each element
  • every - Checks whether all elements satisfy a condition

Sources

Released under the MIT license.