Aller au contenu principal

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);
}