0
0
Typescriptprogramming~20 mins

Generic class with constraints in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic class with constraint
What is the output of this TypeScript code when creating an instance of Container with a Person object?
Typescript
interface HasName {
  name: string;
}

class Container<T extends HasName> {
  private item: T;
  constructor(item: T) {
    this.item = item;
  }
  getName(): string {
    return this.item.name;
  }
}

class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

const person = new Person("Alice");
const container = new Container(person);
console.log(container.getName());
Aundefined
B"Alice"
CTypeError at runtime
DCompilation error due to missing property
Attempts:
2 left
💡 Hint
Look at the constraint T extends HasName and what property Person has.
Predict Output
intermediate
2:00remaining
What error occurs with invalid generic type?
What error will this TypeScript code produce when trying to create a Container with a Car object?
Typescript
interface HasName {
  name: string;
}

class Container<T extends HasName> {
  private item: T;
  constructor(item: T) {
    this.item = item;
  }
  getName(): string {
    return this.item.name;
  }
}

class Car {
  model: string;
  constructor(model: string) {
    this.model = model;
  }
}

const car = new Car("Tesla");
const container = new Container(car);
ACompilation error: Type 'Car' does not satisfy the constraint 'HasName'.
BNo error, but getName() returns undefined
COutput: "Tesla"
DRuntime error: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint
Check if Car has the required name property.
🔧 Debug
advanced
2:00remaining
Why does this generic class cause a runtime error?
This TypeScript code compiles but throws an error at runtime. What causes the error?
Typescript
interface HasName {
  name: string;
}

class Container<T extends HasName> {
  private item: T;
  constructor(item: T) {
    this.item = item;
  }
  printName(): void {
    console.log(this.item.name.toUpperCase());
  }
}

const obj = { name: null };
const container = new Container(obj);
container.printName();
ARuntime error because 'name' is null and has no method 'toUpperCase()'.
BCompilation error because 'name' can be null.
CNo error; prints 'NULL' as string.
DRuntime error because 'item' is undefined.
Attempts:
2 left
💡 Hint
Consider the value of name and what toUpperCase() expects.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this generic class declaration
Which option correctly fixes the syntax error in this TypeScript generic class?
Typescript
interface HasId {
  id: number;
}

class Repository<T HasId> {
  private items: T[] = [];
  add(item: T): void {
    this.items.push(item);
  }
}
ARemove <code>HasId</code> constraint entirely
BChange <code>class Repository<T HasId></code> to <code>class Repository<T implements HasId></code>
CChange <code>T HasId</code> to <code>T extends HasId</code>
DAdd colon: <code>class Repository<T: HasId></code>
Attempts:
2 left
💡 Hint
TypeScript uses a specific keyword for generic constraints.
🚀 Application
expert
2:00remaining
How many items are stored after filtering with generic constraint?
Given this generic class that filters items with a status property, how many items remain after filtering?
Typescript
interface HasStatus {
  status: string;
}

class Filterer<T extends HasStatus> {
  private items: T[];
  constructor(items: T[]) {
    this.items = items;
  }
  filterActive(): T[] {
    return this.items.filter(item => item.status === 'active');
  }
}

const data = [
  { status: 'active', id: 1 },
  { status: 'inactive', id: 2 },
  { status: 'active', id: 3 },
  { status: 'pending', id: 4 }
];

const filterer = new Filterer(data);
const activeItems = filterer.filterActive();
console.log(activeItems.length);
A3
B4
C1
D2
Attempts:
2 left
💡 Hint
Count how many objects have status exactly equal to 'active'.