Deriving Types From Values 15 exercises
Problem

Deriving Types with Classes

Let's revisit the CanvasNode class we worked with previously:


class CanvasNode {
x = 0;
y = 0;
move(x: number, y: number) {
this.x = x;
this.y = y;
}
}

The class has an x attribute and a y attribute, both of which are set to zero by default. Additionally,

Loading exercise

Transcript

00:00 Let's go back in time a little bit. Let's look at our CanvasNode class again. This CanvasNode has an x attribute and a y attribute, both by default set to zero, and a move function which takes in x and y and moves it to that position. Then below we have a function called positionFromCanvasNode.

00:16 And positionFromCanvasNode has a node parameter which is currently typed as any, or implicitly has an any type. Your job is to try to figure out a way to type this positionFromCanvasNode. So what I want you to do is look at the things that are available to you up here, class CanvasNode, things like that, and try to work out,

00:34 given the tools that you already have at your disposal, how you would type this node. We can see down below here we're declaring a new CanvasNode and we're expecting the positionFromCanvasNode to equal x0, y0. Then we move it and we're expecting the positionFromCanvasNode to equal x10, y10.

00:51 So in both situations we're expecting to be able to pass in a CanvasNode into the positionFromCanvasNode function.