What are multiple generic parameters in TypeScript?
They are placeholders for types that allow a function, class, or interface to work with more than one type, making the code flexible and reusable.
Click to reveal answer
beginner
How do you declare a function with two generic parameters in TypeScript?
Use angle brackets with two type variables separated by a comma, like , before the function parameters.
Click to reveal answer
beginner
Example: What does this function do?
function pair(first: T, second: U): [T, U] { return [first, second]; }
It takes two values of any types T and U, and returns a tuple (pair) containing both values in order.
Click to reveal answer
intermediate
Why use multiple generic parameters instead of just one?
Because you might want to work with two or more different types at once, keeping their types separate and flexible.
Click to reveal answer
intermediate
Can generic parameters have constraints when using multiple generics?
Yes, each generic parameter can have its own constraint to limit what types it can accept, improving type safety.
Click to reveal answer
How do you declare a function with two generic types T and U in TypeScript?
Afunction example<T & U>() {}
Bfunction example<T U>() {}
Cfunction example<T, U>() {}
Dfunction example<T; U>() {}
✗ Incorrect
The correct syntax uses a comma between generic parameters inside angle brackets: .
What does this function return? function combine(a: T, b: U): [T, U] { return [a, b]; }
AAn array with elements of types T and U
BA single value of type T
CA single value of type U
DA string combining a and b
✗ Incorrect
It returns a tuple (array) containing the two values with their respective types.
Can each generic parameter have its own constraint in multiple generics?
AYes, each can have different constraints
BNo, constraints apply to all generics together
COnly one generic can have a constraint
DConstraints are not allowed with multiple generics
✗ Incorrect
Each generic parameter can have its own constraint to restrict its type.
Why use multiple generic parameters instead of one?
ATo make code slower
BTo force all types to be the same
CTo avoid using types
DTo handle multiple different types separately
✗ Incorrect
Multiple generics allow working with different types independently.
Which of these is a valid generic function with two parameters?
Afunction f<T; U>(x: T, y: U) {}
Bfunction f<T, U>(x: T, y: U) {}
Cfunction f<T U>(x: T, y: U) {}
Dfunction f<T & U>(x: T, y: U) {}
✗ Incorrect
Only option C uses the correct syntax with a comma separating generic parameters.
Explain how to declare and use a function with multiple generic parameters in TypeScript.
Think about how you can pass two different types and keep them separate.
You got /4 concepts.
Describe why multiple generic parameters are useful and how constraints can be applied to them.
Consider real-life situations where you want to keep two things different but related.
You got /3 concepts.
Practice
(1/5)
1.
What does using multiple generic parameters in TypeScript allow you to do?
Example: function example<T, U>(arg1: T, arg2: U): void {}
easy
A. Only use one type at a time
B. Work with different types together in a single function or class
C. Automatically convert types to strings
D. Disable type checking for function arguments
Solution
Step 1: Understand generic parameters
Generic parameters like T and U let you define placeholders for types.
Step 2: Analyze multiple generics usage
Using multiple generics allows a function or class to accept different types for different parameters, making it flexible and type-safe.
Final Answer:
Work with different types together in a single function or class -> Option B
Quick Check:
Multiple generics = different types together [OK]
Hint: Multiple generics mean multiple type placeholders [OK]
Common Mistakes:
Thinking generics convert types automatically
Assuming only one generic can be used
Believing generics disable type checking
2.
Which of the following is the correct syntax to declare a function with two generic parameters T and U in TypeScript?
easy
A. function example<T & U>(arg1: T, arg2: U): void {}
B. function example<T U>(arg1: T, arg2: U): void {}
C. function example<T; U>(arg1: T, arg2: U): void {}
D. function example<T, U>(arg1: T, arg2: U): void {}
Solution
Step 1: Recall generic syntax
Generic parameters are listed inside angle brackets separated by commas, like <T, U>.
Step 2: Check each option
The correct syntax uses function example<T, U>(arg1: T, arg2: U): void {} with generic parameters separated by commas inside angle brackets. The distractors use invalid delimiters: <T & U>, <T U> (no comma), and <T; U> (semicolon).
Final Answer:
function example<T, U>(arg1: T, arg2: U): void {} -> Option D
Quick Check:
Generics separated by commas inside <> [OK]
Hint: Separate generics with commas inside angle brackets [OK]
Common Mistakes:
Omitting commas between generic parameters
Using semicolons instead of commas
Using & instead of commas
3.
What is the output of the following TypeScript code?
function pairToString<T, U>(pair: [T, U]): string {
return `${pair[0]} and ${pair[1]}`;
}
const result = pairToString<number, string>([5, 'apples']);
console.log(result);
medium
A. "[5, apples]"
B. Type error because types differ
C. "5 and apples"
D. "number and string"
Solution
Step 1: Understand the function behavior
The function takes a tuple with two generic types and returns a string combining both elements.
Step 2: Analyze the call with types and values
Calling with <number, string> and tuple [5, 'apples'] returns the string "5 and apples".
Final Answer:
"5 and apples" -> Option C
Quick Check:
Tuple values combined as string = "5 and apples" [OK]
Hint: Generics keep types but output string combines values [OK]
Common Mistakes:
Expecting a type error due to different types
Thinking output is the tuple stringified
Confusing generic types with runtime values
4.
Identify the error in this TypeScript function using multiple generic parameters:
function combine<T, U>(a: T, b: U): T {
return b;
}
medium
A. Return type is T but function returns b of type U
B. Missing comma between generic parameters
C. Function should not use generics
D. Parameters a and b must be the same type
Solution
Step 1: Check function signature
The function declares it returns type T but returns b which is type U.
Step 2: Identify type mismatch
Returning b violates the declared return type T, causing a type error.
Final Answer:
Return type is T but function returns b of type U -> Option A
Quick Check:
Return type mismatch = error [OK]
Hint: Return type must match returned value type [OK]
Common Mistakes:
Ignoring return type mismatch
Thinking generics auto-convert types
Assuming parameters must share the same type
5.
You want to create a generic function mergeObjects that takes two objects of different types and returns a new object combining both. Which is the correct way to declare this function with multiple generic parameters?
hard
A. function mergeObjects<T, U>(obj1: T, obj2: U): T & U { return {...obj1, ...obj2}; }
B. function mergeObjects<T>(obj1: T, obj2: T): T { return {...obj1, ...obj2}; }
C. function mergeObjects(obj1: object, obj2: object): object { return {...obj1, ...obj2}; }
D. function mergeObjects<T, U>(obj1: T, obj2: U): object { return {...obj1, ...obj2}; }
Solution
Step 1: Understand the goal
The function should accept two objects of different types and return their combined type.
Step 2: Analyze return type
Using T & U as return type means the result has properties from both types, which is correct.
Step 3: Check options
function mergeObjects<T, U>(obj1: T, obj2: U): T & U { return {...obj1, ...obj2}; } correctly uses two generics and returns T & U. function mergeObjects<T>(obj1: T, obj2: T): T { return {...obj1, ...obj2}; } uses one generic and same type for both, which is less flexible. function mergeObjects(obj1: object, obj2: object): object { return {...obj1, ...obj2}; } uses plain objects without generics, losing type safety. function mergeObjects<T, U>(obj1: T, obj2: U): object { return {...obj1, ...obj2}; } returns just object, losing specific type info.
Final Answer:
function mergeObjects<T, U>(obj1: T, obj2: U): T & U { return {...obj1, ...obj2}; } -> Option A
Quick Check:
Multiple generics + intersection return type = correct [OK]
Hint: Use intersection type T & U for combined object return [OK]
Common Mistakes:
Using one generic for both objects
Returning just object losing type info
Not using intersection type for return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.