Multiple Type Parameters
What
TypeScript allows you to use more than one type parameter in your generic definitions, which is essential for logic that operates on two or more distinct types.
How
You list multiple parameters in a comma-separated list within the angle brackets:
function transform<T, U>(input: T, fn: (val: T) => U): U {
return fn(input);
}
const result = transform(5, (n) => n.toString()); // T is number, U is string
Why
Many operations involve converting one type to another (like mapping over an array). Using multiple parameters allows you to precisely track how data changes from its input type to its output type, ensuring that every step of the process remains type-safe.
Summary
Use multiple type parameters to model relationships between different types in your reusable logic.