# Basic Generics

> Passing types as variables.

Subject: TypeScript | Lesson 23 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/basic-generics
Previous: [The this Parameter](https://codewithanimation.in/typescript/this-parameter.md) | Next: [Generic Constraints](https://codewithanimation.in/typescript/generic-constraints.md)

## Variables for Types

Generics allow you to write reusable code that works with any data type, without losing strict type checking.

Just like a function takes a *value* variable as an argument, a generic function takes a *type* variable (usually called `<T>`) as an argument.

## The 'any' trap

If you write a function `getFirstItem(arr: any[]): any`, it accepts anything, but when it returns the first item, you have no idea what type that item is. You lost the type safety.

If you use generics `getFirstItem<T>(arr: T[]): T`, TypeScript tracks exactly what type you passed in. If you pass an array of numbers, it guarantees the return type is a number.

## Syntax

```typescript
// <T> declares the type variable.
// We use T for the array argument, and T for the return type.
function getFirstItem<T>(arr: T[]): T | undefined {
  return arr[0];
}

// Explicitly passing the type (optional, but clear):
const a = getFirstItem<number>([1, 2, 3]); // 'a' is number

// Implicitly inferred by TypeScript:
const b = getFirstItem(["apple", "banana"]); // 'b' is string
```

## Common mistake: Assuming too much

A common error is trying to access properties on a generic type: `function logLen<T>(arg: T) { console.log(arg.length); }`

TypeScript will throw an error because it doesn't know if `T` has a `.length` property. `T` could be a boolean! To fix this, you must "constrain" the generic (covered in the next concept).

## Tip: Naming Conventions

While `T` (Type) is standard for a single generic, use descriptive names for multiple generics or complex types: `<TValue, TError>`, or `<TState>`.

## Try it: Try it

Select a type to pass into the generic function. Watch how the type variable `<T>` cascades through the signature, replacing the placeholders and locking the return type.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/basic-generics

## Check yourself

### Question 1

Why are Generics better than using the 'any' type for a reusable function?

- A. Generics run faster in the browser.
- B. Generics preserve the original type information across the function signature.
- C. Generics automatically convert the data to the correct type.

Correct answer: B

Using 'any' acts as a black hole, erasing type data. Generics act as a variable, passing the specific type data through the function from input to output.

- Why not A: Generics are erased at compile time; they have zero impact on runtime speed.
- Why not C: TypeScript types do not alter runtime data.

## Remember this

- Generics `<T>` act as variables for types.
- They allow a function to adapt to any type while maintaining strict checking.
- The compiler can often infer `<T>` automatically from the arguments you pass.
- Generics prevent the loss of type safety caused by `any`.
