function identity<T>(value: T): T { return value; } // Using the generic function let result = identity<number>(5); // here, T is replaced with number console.log(result); // Output: 5 let value = identity<string>("Hello"); // here, T is replaced with string console.log(value.toUpperCase());
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter