Here is a TypeScript class example:
class Vector3 {
//-----PUBLIC VARS
x: number;
y: number;
z: number;
//-----CONSTRUCTORS
constructor(x?: number, y?: number, z?: number) {
this.x = x == undefined ? 0 : x;
this.y = y == undefined ? 0 : y;
this.z = z == undefined ? 0 : z;
}
//-----METHODS
SetValues(x: number, y: number, z: number): void {
this.x = x;
this.y = y;
this.z = z;
}
}
Now, let's write a unit test code for the Vector3 class:
function TestVector3() {
var lhs = new Vector3(1, 2, 3);
lhs.SetValues(1, 2, 3);
if (lhs.x != 2 || lhs.y != 6 || lhs.z != 12) {
throw "a";
}
lhs.SetValues(-1, 2, 3);
if (lhs.x != -1 || lhs.y != -2 || lhs.z != -3) {
throw "b";
}
}
An error occurs on 'lhs.x != -1' with the message TS2367. Removing the line 'throw "a";' resolves this error. How can we address this issue?
This error should not occur, and I find it quite frustrating...