0
0
Typescriptprogramming~20 mins

Getter and setter with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Getter and Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript getter and setter example?

Consider this TypeScript class with typed getter and setter. What will be printed when the code runs?

Typescript
class Person {
  private _age: number = 0;

  get age(): number {
    return this._age;
  }

  set age(value: number) {
    if (value < 0) {
      this._age = 0;
    } else {
      this._age = value;
    }
  }
}

const p = new Person();
p.age = -5;
console.log(p.age);
A0
B-5
Cundefined
DTypeError
Attempts:
2 left
💡 Hint

Look at how the setter handles negative values.

Predict Output
intermediate
2:00remaining
What is the output when accessing a getter without a setter?

Look at this TypeScript class with only a getter. What happens when we try to assign a value to the property?

Typescript
class Circle {
  private _radius: number = 5;

  get radius(): number {
    return this._radius;
  }
}

const c = new Circle();
c.radius = 10;
console.log(c.radius);
A5
B10
CCompilation error
DTypeError at runtime
Attempts:
2 left
💡 Hint

Think about whether you can assign to a getter-only property in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this setter cause a stack overflow error?

Examine this TypeScript class. Why does setting value cause a stack overflow?

Typescript
class Counter {
  private _count = 0;

  get count(): number {
    return this._count;
  }

  set count(value: number) {
    this.count = value; // Problem here
  }
}

const counter = new Counter();
counter.count = 5;
ABecause the setter calls itself recursively without a base case
BBecause the getter is missing a return statement
CBecause _count is not initialized
DBecause count is declared private
Attempts:
2 left
💡 Hint

Look at what happens inside the setter when assigning this.count = value.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a typed getter and setter in TypeScript?

Choose the option that correctly defines a getter and setter for a name property of type string.

A
class User {
  private _name: string = '';
  get name(): string {
    return this._name;
  }
  set name(value: string) {
    return this._name = value;
  }
}
B
class User {
  private _name: string = '';
  get name() {
    return this._name;
  }
  set name(value) {
    this._name = value;
  }
}
C
class User {
  private _name: string = '';
  get name(): string {
    return this._name;
  }
  set name(value: number) {
    this._name = value;
  }
}
D
class User {
  private _name: string = '';
  get name(): string {
    return this._name;
  }
  set name(value: string) {
    this._name = value;
  }
}
Attempts:
2 left
💡 Hint

Check the types of getter return and setter parameter, and the setter body.

🚀 Application
expert
2:00remaining
How many items are in the resulting array after using a getter that returns a filtered list?

Given this TypeScript class with a getter that filters items, how many items does filteredItems contain?

Typescript
class Store {
  private items: number[] = [1, 2, 3, 4, 5, 6];

  get filteredItems(): number[] {
    return this.items.filter(x => x % 2 === 0);
  }
}

const store = new Store();
const filtered = store.filteredItems;
A6
B3
C4
D0
Attempts:
2 left
💡 Hint

Count how many even numbers are in the original array.