Typescript Classes 10 exercises
Problem

Setter Methods in TypeScript

Continuing on with the CanvasNode, we have some read-only variables #x and #y along with a constructor, a position getter, and a move function. Here's our class:


class CanvasNode {
#x: number;
#y: number;
constructor(position?: { x: number; y: number }) {
this.#x = posi

Loading exercise

Transcript

00:00 We're back with our canvas node. We've got our read-only properties with our hash. We have a constructor, we have a get position, and we have a move function. We now have a new requirement where we want to be able to modify the position from outside directly. Now this is an issue because if we hover over this,

00:17 you can see that we cannot assign to position because it's a read-only property. Getters, by default, they are read-only because it just doesn't make sense to assign to a getter like this. Your job is to try to work out what API we need to add to our class in order to make this mutable,

00:36 and also to try to figure out the logic of what you would even do to kind of figure this out, right? What logic needs to be added to this class in order to actually assign it into these private properties? And I think you're gonna need a setter. Good luck.