Skip to main content

identity()

identity<T>(value): T

Returns the first argument it receives.

DEPRECATED

Use an inline arrow function (x) => x instead.


Type Parametersโ€‹

T: Tโ€‹

The type of the value.


Parametersโ€‹

value: Tโ€‹

Any value.


Returns: Tโ€‹

The value.


Sinceโ€‹

2.0.0


Also known asโ€‹

identity (Lodash, es-toolkit, Remeda, Ramda, Effect) ยท โŒ (Radashi, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
[1, 2, 3].filter(identity);

// โœ… Recommended approach
[1, 2, 3].filter(x => x);
// Or simply
[1, 2, 3].filter(Boolean);

How it works?โ€‹

Returns the first argument it receives. Deprecated: Use an inline arrow function (x) => x instead.

Native Equivalentโ€‹

// โŒ arr.filter(identity)
// โœ… arr.filter(x => x)
// โœ… arr.filter(Boolean)

Use Casesโ€‹

Pass-through function ๐Ÿ“Œโ€‹

Return value unchanged.

const fn = (x: T) => x;
// Or for filtering truthy:
[1, 0, 2, null].filter(Boolean); // [1, 2]

Default transformerโ€‹

Use as default when no transform needed.

function process<T>(items: T[], transform = (x: T) => x) {
return items.map(transform);
}