Menu
Coddy logo textTech

The Problem Generics Solve

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 57 of 73.

Imagine you want to create a function that simply returns whatever value you pass to it - an "identity" function. In regular JavaScript, you might write something like this:

function identity(arg: any): any {
  return arg;
}

While this function works, using any creates a significant problem: you lose all type safety. When you call identity("hello"), TypeScript can't tell you that the result is a string. It only knows the result is any, which means you lose autocompletion, type checking, and all the benefits TypeScript provides.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript