简体   繁体   English

TypeScript中是否有回调函数类型?

[英]Is there a type for callback functions in TypeScript?

在此输入图像描述 The error says Supplied parameter do not match any signature of call target . 错误说Supplied parameter do not match any signature of call target When I replace Function with any as the second parameter's type, the error disappears. 当我将Function替换为any作为第二个参数的类型时,错误消失。 But any is the same as no type, isn't there a suitable type for functions as parameters? 但是any类型都没有类型,作为参数的功能是不是合适的类型?

Instead of Function (or any ) you can use the following type for your callback parameter: 您可以使用以下类型作为callback参数而不是Function (或any ):

(ev: Event)=> any

This matches the type expected by addEventListener . 这与addEventListener期望的类型匹配。

Here is the full function signature: 这是完整的功能签名:

on(eventName: string, callback: (ev: Event)=> any, useCapture: boolean) : Dom.Element {
    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM