Skip to content

Instantly share code, notes, and snippets.

@jdomingu19
Created April 6, 2026 06:45
Show Gist options
  • Select an option

  • Save jdomingu19/290070311fef671e69e1cee2288b282f to your computer and use it in GitHub Desktop.

Select an option

Save jdomingu19/290070311fef671e69e1cee2288b282f to your computer and use it in GitHub Desktop.
// Hello, TypeScript! @jdomingu19
// Playground 005: Generic Function
(() => {
/**
* A generic function that returns the same value it receives.
*
* @typeParam T - The type of the input and output value.
* @param value - The value to be returned, of type `T`.
* @returns The same value provided as input, preserving its type.
*/
function identify<T>(value: T): T {
return value;
}
// Using the generic function with a number and string
const num = identify<number>(42); // number
const str = identify<string>("Marowak"); // string
// Logs the value and its type to the console
console.log({ num }, typeof num); // { num: 42 } number
console.log({ str }, typeof str); // { str: 'Marowak' } string
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment