Challenge - 5 Problems
Advanced Generics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a generic function with constraints
What is the output of this TypeScript code using generics with constraints?
Typescript
function merge<T extends object, U extends object>(obj1: T, obj2: U): T & U { return {...obj1, ...obj2}; } const result = merge({name: "Alice"}, {age: 30}); console.log(result);
Attempts:
2 left
💡 Hint
Think about how the spread operator combines objects and how generics with constraints work.
✗ Incorrect
The function merges two objects using spread syntax. The generic constraints ensure both inputs are objects. The result combines properties from both inputs.
🧠 Conceptual
intermediate1:30remaining
Why use advanced generics in TypeScript?
Which of the following best explains why advanced generics matter in TypeScript?
Attempts:
2 left
💡 Hint
Think about how generics help with type safety and code reuse.
✗ Incorrect
Advanced generics let you write code that adapts to different types while keeping type checks, making code reusable and safe.
🔧 Debug
advanced2:00remaining
Identify the error in this generic function
What error does this TypeScript code produce?
Typescript
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } const person = {name: "Bob", age: 25}; const value = getProperty(person, "height");
Attempts:
2 left
💡 Hint
Check the constraint on the key parameter and the keys of the object.
✗ Incorrect
The generic constraint K extends keyof T means the key must be a valid property name of T. 'height' is not a key of person, so TypeScript raises a compile error.
📝 Syntax
advanced1:30remaining
Which option correctly defines a generic type with default?
Which TypeScript code correctly defines a generic interface with a default type parameter?
Attempts:
2 left
💡 Hint
Look for the correct syntax for default generic parameters in TypeScript.
✗ Incorrect
Option D uses the correct syntax to set a default type parameter for a generic interface.
🚀 Application
expert2:30remaining
Determine the output of a complex generic mapped type
What is the type of 'Result' after this code runs?
Typescript
type Options = { a: number; b: string; c: boolean };
type Nullable<T> = { [P in keyof T]: T[P] | null };
type Result = Nullable<Pick<Options, 'a' | 'c'>>;Attempts:
2 left
💡 Hint
Understand how Pick and mapped types work together with generics.
✗ Incorrect
Pick selects only 'a' and 'c' properties. Nullable makes each property nullable. So Result has 'a' and 'c' keys with their types or null.