通过函数重载进行销毁

时间:2019-01-16 04:40:11

标签: typescript destructuring

我正在尝试创建一个函数,该函数采用一对坐标或具有xy属性的对象,并返回邻居列表。但是由于某种原因,即使检查了对象的类型,我也无法对其进行销毁:

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;
}

当然,我可以简单地使用x = a.x; y = a.y;,但我很感兴趣-如何进行这种解构工作?

3 个答案:

答案 0 :(得分:2)

need to destructuring declaration and to access global scope use var instead of let.

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') {
   var {x, y} = a as Coords; // A destructuring declaration must have an initializer
   x = x;
   y = y;
   console.log('inside destructuring: ', x, y);
 } else {
    x = a;
    y = b as number;
 }
 console.log('outside destructuring: ', x,y);
 const result = [{ x: x - 1, y }, { x: x + 1, y }, { x, y: y - 1 }, { x, y: y + 1 }];
 return result;
}

在此处StackBlitz example

可以避免var并将对象声明为全局对象。

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;
 let globalStorage: any = {};
 if (typeof a === 'object') {
   let {x, y} = a as Coords; // A destructuring declaration must have an initializer
   globalStorage.x = x;
   globalStorage.y = y;
   console.log('inside destructuring: ', x, y);
  } else {
    globalStorage.x = a;
    globalStorage.y = b as number;
  }
 console.log('outside destructuring: ', globalStorage);
 const result = [{ x: globalStorage.x - 1, y:globalStorage.y }, { x: globalStorage.x + 1, y:globalStorage.y }, { x: globalStorage.x, y: globalStorage.y - 1 }, { x: globalStorage.x, y: globalStorage.y + 1 }];
 return result;
}

答案 1 :(得分:1)

似乎有点复杂,为什么不反转逻辑,对我来说更容易理解。

CodePen.IO Example

console.clear();
class Foo
{
  getNeighbours(coords: ICoords): ICoords[]; 
  getNeighbours(x: number, y: number) : ICoords[];
  getNeighbours(coords: number | ICoords, y?: number) : ICoords[]{
    if (typeof coords !=="object") {
      coords = { x: coords, y: y};
    }
    const result = [
      { x: coords.x - 1, y: coords.y }, 
      { x: coords.x + 1, y: coords.y }, 
      { x: coords.x, y: coords.y - 1 }, 
      { x: coords.x, y: coords.y + 1 }];
    return result;
  }
}

interface ICoords {
  x: number;
  y: number;
}

let foo = new Foo();
//debugger;
console.log(foo.getNeighbours(1,1));
console.log(foo.getNeighbours({x:1,y:1}));

答案 2 :(得分:0)

销毁之前缺少 public ApiController(IUserService userService,HumanDBContext db) { _db = db; _userService = userService; }

let