I am learning TypeScript with a background in Java, where I am used to being able to easily work with hashmaps and hashsets with objects by overriding the hashCode method. Is there a way to do a similar thing in TypeScript?
class Coord {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
toString(): string {
return ((this.x + this.y) * (this.x + this.y + 1)/2).toString();
}
hashCode(): number {
return (this.x + this.y) * (this.x + this.y + 1)/2;
}
}
const f = () => {
let visited: Set<Coord> = new Set();
visited.add(new Coord(1, 1));
visited.add(new Coord(1, 1));
console.log(visited.size)
}
f()
I have tried modifying toString and hashCode, but I still get that visited.size is 2.