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

Multiple Type Parameters

Using two or more type parameters in a single function to handle relationships between different types.

Multiple Type Parameters

You'll learn: How to use more than one type parameter in a single function to preserve and relate different types simultaneously.

The Core Idea

So far, you've seen generic functions with a single type parameter like <T>. But what if your function needs to handle two different types that aren't the same? That's where multiple type parameters come in — you can use <T, U>, <A, B>, or any combination of type parameter names to track multiple distinct types through the same function.

Why You Need Multiple Type Parameters

Imagine a function that creates a pair of values — like packaging a username (string) with an age (number), or any two items that might have completely different types. A single type parameter can't capture both.

With multiple type parameters, TypeScript preserves the type of each argument independently:

function createPair<T, U>(first: T, second: U): [T, U] {
  return [first, second];
}

const pair1 = createPair("Alice", 30);        // [string, number]
const pair2 = createPair(true, "active");     // [boolean, string]
const pair3 = createPair(42, { id: 5 });      // [number, { id: number }]

Each call infers T and U separately based on what you pass.

Common Patterns

Multiple type parameters shine when mapping, transforming, or combining values:

function mapValue<T, U>(value: T, transform: (input: T) => U): U {
  return transform(value);
}

const length = mapValue("hello", (s) => s.length);  // U inferred as number
const upper = mapValue("hello", (s) => s.toUpperCase());  // U inferred as string

Key Takeaway: Multiple type parameters let you track relationships between distinct types in a single function, preserving full type safety for each independently.