具有回调属性的Typescript接口

时间:2018-09-27 09:48:40

标签: typescript types interface

如何将robotCall方法的参数中的回调runRobot提取到接口中?我已经尝试过

interface RobotCall {
  call: ((state: VillageState, memory: string[]) => Robot)
}

但是请继续获取TypeError。这是正在使用的完整功能,我想在其上使用该接口。

export function runRobot(
  state: VillageState,
  robotCall: ((state: VillageState, memory: string[]) => Robot),
  memory?: string[]): void {

  for (let turn = 0; ; turn++) {
    if (state.parcels.length === 0) {
      console.log('='.repeat(5), `${robotCall.name} done in ${turn} turns`, '='.repeat(5));
      break;
    }

    let robot = robotCall(state, memory);
    state = state.move(robot.direction);
    memory = robot.memory;
  }
}

0 个答案:

没有答案