Function/Since
1.38.0identity
Returns the given value unchanged.
identity(value):TParameters
valueTrequiredThe value to return.
Returns
TReturns the exact value passed to the function.
Examples
01
Basic usage
Returns the provided number unchanged.
TypeScript
const result = identity(42);
console.log(result);
// 4202
With strings
The function preserves the string value and its type.
TypeScript
const result = identity("Hello");
console.log(result);
// "Hello"03
With objects
Objects are returned as the same value passed to the function.
TypeScript
const user = {
name: "Amir",
age: 20,
};
const result = identity(user);
console.log(result);
// { name: "Amir", age: 20 }04
Generic type inference
TypeScript automatically infers T from the provided value.
TypeScript
const numberValue = identity(123);
// T is inferred as number
const stringValue = identity("CyrefJS");
// T is inferred as string
const booleanValue = identity(true);
// T is inferred as boolean05
Explicit generic type
You can explicitly provide the generic type when needed.
TypeScript
const result = identity<number>(42);
console.log(result);
// 42Built with@cyref/js·Official documentation