I recently created a Vector class but I am encountering some issues with the syntax.
Here is the code snippet:
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);
}
}
The issue lies in the syntax here:
https://i.sstatic.net/KPdA3.png
In the statement "return new Vector(x, y)", the error message appears as "Variable 'x' is used before being assigned". Additionally, when referencing "v[component]", the error states that "Type 'Extract<keyof this, string>' cannot be used to index type 'Vector'."
Although the code functions properly, it's important for me to rectify these syntax errors and write it correctly.