assemblyscript
assemblyscript copied to clipboard
Support eq / ne when LHS and RHS both nullable
Simple example:
class Vec2 {
constructor(public x: f64 = 0, public y: f64 = 0) {}
@operator("==")
eq(other: Vec2): bool {
return this.x == this.x && other.y == other.y;
}
}
class Expect<T> {
constructor(public actual: T) {}
public toBe(expected: T): bool {
let actual = this.actual;
// ERROR TS2322: Type 'module/Vec2 | null' is not assignable to type 'module/Vec2'.
return actual == expected;
}
}
const exp = new Expect<Vec2 | null>(new Vec2(1, 2)); // <- nullable
assert(exp.toBe(new Vec2(1, 2)) == true);
However, we could improve this and don't force use exclamation marks or type narrowing for such cases. And add extra checks automatically before actual calling overloaded method.