0
0
Typescriptprogramming~20 mins

Why advanced generics matter in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced Generics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A{"name": "Alice", "age": 30}
B{"name": "Alice"}
CTypeError at runtime
DSyntaxError during compilation
Attempts:
2 left
💡 Hint
Think about how the spread operator combines objects and how generics with constraints work.
🧠 Conceptual
intermediate
1:30remaining
Why use advanced generics in TypeScript?
Which of the following best explains why advanced generics matter in TypeScript?
AThey automatically fix runtime errors without extra code.
BThey make the code run faster by optimizing JavaScript output.
CThey allow writing flexible and reusable code that works with many types while preserving type safety.
DThey replace the need for interfaces and classes entirely.
Attempts:
2 left
💡 Hint
Think about how generics help with type safety and code reuse.
🔧 Debug
advanced
2: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");
ACompilation error: Argument of type '"height"' is not assignable to parameter of type '"name" | "age"'
BTypeError at runtime because 'height' does not exist
CNo error, outputs undefined
DSyntaxError due to missing return type
Attempts:
2 left
💡 Hint
Check the constraint on the key parameter and the keys of the object.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a generic type with default?
Which TypeScript code correctly defines a generic interface with a default type parameter?
Ainterface Box<T> = string { value: T; }
Binterface Box<T: string> { value: T; }
Cinterface Box<T default string> { value: T; }
Dinterface Box<T = string> { value: T; }
Attempts:
2 left
💡 Hint
Look for the correct syntax for default generic parameters in TypeScript.
🚀 Application
expert
2: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'>>;
A{"a": number, "c": boolean}
B{"a": number | null, "c": boolean | null}
C{"a": number | null, "b": string | null, "c": boolean | null}
D{"a": number, "b": string, "c": boolean | null}
Attempts:
2 left
💡 Hint
Understand how Pick and mapped types work together with generics.