Challenge - 5 Problems
Generic Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic interface usage
What is the output of this TypeScript code when compiled and run with Node.js (ignoring type checks)?
Typescript
interface Box<T> {
content: T;
}
const box: Box<number> = { content: 123 };
console.log(box.content);Attempts:
2 left
💡 Hint
Look at the value assigned to content and what is logged.
✗ Incorrect
The interface Box defines a generic type with a content property of type T. Here T is number, so content is 123. Logging box.content prints 123.
❓ Predict Output
intermediate2:00remaining
Output of generic interface with union type
What will be the output of this code snippet?
Typescript
interface Result<T> {
success: boolean;
data: T;
}
const res: Result<string | null> = { success: true, data: null };
console.log(res.data);Attempts:
2 left
💡 Hint
Check the assigned value and the union type.
✗ Incorrect
The data property can be string or null. Here it is null, so logging prints null.
🔧 Debug
advanced2:00remaining
Identify the error in generic interface usage
What error will this TypeScript code produce?
Typescript
interface Pair<T, U> {
first: T;
second: U;
}
const pair: Pair<number> = { first: 1, second: 2 };Attempts:
2 left
💡 Hint
Check how many generic parameters Pair expects.
✗ Incorrect
Pair expects two generic types T and U, but only one is provided. This causes a compile-time error.
❓ Predict Output
advanced2:00remaining
Output of nested generic interfaces
What is the output of this code?
Typescript
interface Container<T> {
value: T;
}
interface Wrapper<U> {
container: Container<U>;
}
const wrapped: Wrapper<string> = { container: { value: "hello" } };
console.log(wrapped.container.value);Attempts:
2 left
💡 Hint
Look at the nested generic types and the assigned values.
✗ Incorrect
Wrapper has a container property of type Container. Here U is string, so container.value is "hello". Logging it prints "hello".
🧠 Conceptual
expert2:00remaining
Number of properties in a generic interface instance
Given this generic interface and instance, how many properties does the object 'item' have at runtime?
Typescript
interface Item<T> {
id: number;
data: T;
timestamp?: Date;
}
const item: Item<string> = { id: 1, data: "abc" };Attempts:
2 left
💡 Hint
Optional properties may not exist on the object.
✗ Incorrect
The object has 'id' and 'data' properties defined. 'timestamp' is optional and not present, so total properties are 2.