I made a Vector class but I have some problem with the syntax.
This is my code:
export class Vector {
x: number;
y: number;
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(v: Vector) {
var x: number, y: number;
for (var component in this) {
component == "x"
? (x = this[component] + v[component])
: (y = this[component] + v[component]);
}
return new Vector(x, y);
}
}
Here is the syntax problem I have:
As you see "x" and "y" in line "return new Vector(x,y)" say "Variable 'x' is used before being assigned"... And for the "v[component]" says "Type 'Extract<keyof this, string>' cannot be used to index type 'Vector'."
I don't know what I have done wrong, the code works but I want to write it correctly.

ifstatement is the logical choice. There's literally no reason to do so.if (component === "x") { /*x = ...*/ } else { /*y = ...*/ }.