Multiple Type Parameters with Different Constraints
You'll learn: How to define generic functions with several type parameters where each has its own independent constraint.
What This Means
Sometimes you need a function that works with multiple generic types, but each type has different requirements. TypeScript lets you apply a separate extends constraint to each type parameter independently.
Think of it like a restaurant with different requirements for different ingredients: your vegetables must be fresh (one constraint), your meat must be certified (another constraint), and your spices must be organic (yet another). Each ingredient category has its own rules.
The Core Idea
You've already seen single type parameters with constraints like <T extends string>. When you have multiple type parameters, you can give each one its own constraint:
function merge<T extends object, U extends object>(obj1: T, obj2: U) {
return { ...obj1, ...obj2 };
}
const result = merge({ name: "Alice" }, { age: 30 });
// result is { name: string, age: number }
Here T must be an object, and U must be an object — both constrained, but independently.
You can also mix constrained and unconstrained parameters, or use completely different constraints:
function wrap<T extends string, U extends number>(text: T, count: U) {
return { text, count };
}
const wrapped = wrap("hello", 5);
Key Takeaway: Each type parameter in a generic function can have its own extends constraint, allowing you to enforce different requirements for different generic types in the same function signature.