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