Multiple generic parameters in Typescript - Mini Project: Build & Apply
makePair with two generic parameters T and U. The function should take two arguments: first of type T and second of type U. It should return an object with properties first and second holding these values.Use after the function name to declare two generic types.
makePair with string and number typesmakePair function with "Alice" as the first argument and 30 as the second argument. Store the result in a variable called person.Specify the types string and number when calling makePair.
makePair with boolean and string typesmakePair function with true as the first argument and "active" as the second argument. Store the result in a variable called status.Use boolean and string as the generic types for this call.
person and status to the console using two separate console.log statements.Use console.log(person) and console.log(status) to show the results.
Practice
What does using multiple generic parameters in TypeScript allow you to do?
Example: function example<T, U>(arg1: T, arg2: U): void {}
Solution
Step 1: Understand generic parameters
Generic parameters likeTandUlet 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 BQuick Check:
Multiple generics = different types together [OK]
- Thinking generics convert types automatically
- Assuming only one generic can be used
- Believing generics disable type checking
Which of the following is the correct syntax to declare a function with two generic parameters T and U in TypeScript?
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 usesfunction 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 DQuick Check:
Generics separated by commas inside <> [OK]
- Omitting commas between generic parameters
- Using semicolons instead of commas
- Using & instead of commas
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);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 CQuick Check:
Tuple values combined as string = "5 and apples" [OK]
- Expecting a type error due to different types
- Thinking output is the tuple stringified
- Confusing generic types with runtime values
Identify the error in this TypeScript function using multiple generic parameters:
function combine<T, U>(a: T, b: U): T {
return b;
}Solution
Step 1: Check function signature
The function declares it returns typeTbut returnsbwhich is typeU.Step 2: Identify type mismatch
Returningbviolates the declared return typeT, causing a type error.Final Answer:
Return type is T but function returns b of type U -> Option AQuick Check:
Return type mismatch = error [OK]
- Ignoring return type mismatch
- Thinking generics auto-convert types
- Assuming parameters must share the same type
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?
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
UsingT & Uas 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 returnsT & 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 justobject, losing specific type info.Final Answer:
function mergeObjects<T, U>(obj1: T, obj2: U): T & U { return {...obj1, ...obj2}; } -> Option AQuick Check:
Multiple generics + intersection return type = correct [OK]
- Using one generic for both objects
- Returning just object losing type info
- Not using intersection type for return
Ready to prove it?
Pass the test and this pattern moves unknown
