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