Challenge - 5 Problems
Master of Multiple Generic Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a function with two generic parameters
What is the output of the following TypeScript code?
Typescript
function pairToString<T, U>(first: T, second: U): string { return `${typeof first}:${first},${typeof second}:${second}`; } console.log(pairToString<number, string>(42, "hello"));
Attempts:
2 left
💡 Hint
Look at how the function uses typeof to get the type names as strings.
✗ Incorrect
The function returns a string showing the type and value of each parameter. For 42 (number) and "hello" (string), it returns "number:42,string:hello".
❓ Predict Output
intermediate2:00remaining
Result of generic tuple type usage
What is the type of variable 'result' after this code runs?
Typescript
function makeTuple<A, B>(a: A, b: B): [A, B] { return [a, b]; } const result = makeTuple<string, number>("age", 30);
Attempts:
2 left
💡 Hint
Check the return type annotation of the function.
✗ Incorrect
The function returns a tuple with the first element of type A and the second of type B. Here, A is string and B is number, so the type is [string, number].
🔧 Debug
advanced2:00remaining
Identify the error in multiple generic parameters usage
What error does this TypeScript code produce?
Typescript
function combine<T, U>(a: T, b: U): T & U { return { ...a, ...b }; } const result = combine<number, string>(5, "hello");
Attempts:
2 left
💡 Hint
Consider what types can be spread into objects.
✗ Incorrect
The spread operator ... works only on objects. Number and string are primitive types, so spreading them causes a TypeError.
📝 Syntax
advanced2:00remaining
Correct syntax for multiple generic parameters in interface
Which option correctly declares an interface with two generic parameters T and U?
Attempts:
2 left
💡 Hint
Generic parameters are separated by commas inside angle brackets.
✗ Incorrect
Option A uses correct syntax: angle brackets with comma-separated generic parameters.
🚀 Application
expert3:00remaining
Determine output of complex generic function with constraints
What is the output of this TypeScript code?
Typescript
function mergeObjects<T extends object, U extends object>(obj1: T, obj2: U): T & U { return { ...obj1, ...obj2 }; } const a = { name: "Alice" }; const b = { age: 25 }; const merged = mergeObjects(a, b); console.log(merged.name + " is " + merged.age + " years old.");
Attempts:
2 left
💡 Hint
Check how the function merges two objects and the constraints on generic types.
✗ Incorrect
The function merges two objects with generic constraints ensuring both are objects. The merged object has properties from both, so accessing merged.name and merged.age works.