I'm trying to make a function which takes either a pair of coords or an object with x and y properties and returns a list of neighbors. But for some reason I cannot destructurise the object, even when I checked its type:
interface Coords {
x: number;
y: number;
}
public getNeighbours(coords: Coords): Coords[];
public getNeighbours(x: number, y: number): Coords[];
public getNeighbours(a: number | Coords, b?: number): Coords[] {
let x: number;
let y: number;
if (typeof a === 'object') {
{ x, y } = a as Coords; // failing
} else {
x = a;
y = b as number;
}
const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
return result;
}
Of course, I can simply use x = a.x; y = a.y;, but I'm interested - how can I make this destructuring work?