0
0
Typescriptprogramming~20 mins

Generic interface declaration in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A123
Bundefined
CError: Type 'string' is not assignable to type 'number'.
Dnull
Attempts:
2 left
💡 Hint
Look at the value assigned to content and what is logged.
Predict Output
intermediate
2: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);
AError: Type 'null' is not assignable to type 'string'.
Bundefined
C"null"
Dnull
Attempts:
2 left
💡 Hint
Check the assigned value and the union type.
🔧 Debug
advanced
2: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 };
AError: Property 'second' is missing in type '{ first: number; }'.
BError: Generic type 'Pair<T, U>' requires 2 type arguments.
CError: Type 'number' is not assignable to type 'U'.
DNo error, compiles fine.
Attempts:
2 left
💡 Hint
Check how many generic parameters Pair expects.
Predict Output
advanced
2: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);
A"hello"
Bundefined
CError: Type 'number' is not assignable to type 'string'.
Dnull
Attempts:
2 left
💡 Hint
Look at the nested generic types and the assigned values.
🧠 Conceptual
expert
2: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" };
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Optional properties may not exist on the object.