打字稿如何定义回调函数?

时间:2017-07-27 12:54:53

标签: typescript

我的功能如下:

 .add({a: 1, b: 2}, function (msg, reply) {
    reply({z: msg.z})
  })

它试过这样的事情:

interface SenecaMethods {
    add: (patern: object, HERE SHOULD BE CALLBACK) => object;
}

如何为此定义类型?

1 个答案:

答案 0 :(得分:0)

add定义类型的方式相同 - 函数类型对于回调看起来与对任何其他类型的函数相同。把它放在括号内:

interface SenecaMethods {
    // Obviously you should replace the 'any's with your actual types!
    add: (patern: object, cb: (msg: any, reply: any) => any) => object;
}