Course contentsShow
← TypeScript
Lesson 534 of 1,79613. GenericsFree lesson

Type Parameters as Variables

Thinking of type parameters as variables that hold types, just like regular variables hold values.

Type Parameters as Variables

You'll learn to think of type parameters like T as containers for types, just as regular variables are containers for values.

The Mental Model Shift

When you first encounter code like function identity<T>(value: T): T, it's easy to get lost in the syntax. But here's the key insight: T works exactly like a variable, except it holds a type instead of a value.

Consider this parallel:

// Regular variable holds a VALUE
let x = 42;

// Type parameter holds a TYPE
function wrap<T>(value: T) {
  return value;
}

Just as x stores the number 42, the type parameter T stores whatever type you pass in — like string, number, or boolean.

Assignment and Flow

When you call a generic function, TypeScript "assigns" a type to your type parameter:

function first<T>(items: T[]): T {
  return items[0];
}

const num = first([1, 2, 3]);     // T is "assigned" number
const str = first(['a', 'b']);    // T is "assigned" string

In the first call, T = number. In the second, T = string. The type parameter gets its value from the argument you pass, just like how a regular parameter gets its value from your function call.

Why This Matters

Once you see type parameters as variables, generics become less mysterious. You're not doing magic — you're just letting TypeScript fill in the blanks based on what you pass in. The type "flows" through your function the same way a value would.

Key Takeaway: Type parameters are variables that hold types instead of values, assigned when you call a generic function and used consistently throughout its body.