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

Multiple Type Parameters in Interfaces

Use multiple type parameters in a single interface to handle complex relationships between types.

Multiple Type Parameters in Interfaces

You'll learn how to define interfaces with two or more type parameters to model complex relationships between different types.

Why Multiple Type Parameters?

Sometimes a single generic type parameter isn't enough. Imagine you're building a key-value pair structure or a function that transforms one type into another. You need to track both types independently — that's where multiple type parameters shine.

Think of it like a recipe that needs two ingredient lists: one for the batter and one for the frosting. Each list is completely independent but both are part of the same cake interface.

Basic Syntax

interface Pair<T, U> {
  first: T;
  second: U;
}

const nameAndAge: Pair<string, number> = {
  first: "Alice",
  second: 30
};

const coordinates: Pair<number, number> = {
  first: 10,
  second: 20
};

The <T, U> syntax declares two independent type parameters. T and U can be any types, and they don't have to match.

Real-World Example

interface Result<TSuccess, TError> {
  success: boolean;
  value?: TSuccess;
  error?: TError;
}

const goodResult: Result<number, string> = {
  success: true,
  value: 42
};

const badResult: Result<number, string> = {
  success: false,
  error: "Connection failed"
};

Here, TSuccess tracks the success type while TError tracks the error type — completely independent concerns.

Key Takeaway: Multiple type parameters let interfaces handle complex relationships where different properties or operations involve different, independent types. Use them when one generic type parameter isn't expressive enough.