如何为方法参数中使用的函数回调定义类型(作为任何函数类型,而不是通用类型) [英] How to define type for a function callback (as any function type, not universal any) used in a method parameter
本文介绍了如何为方法参数中使用的函数回调定义类型(作为任何函数类型,而不是通用类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我的类型定义为:
interface Param {
title: string;
callback: any;
}
我需要类似的东西:
interface Param {
title: string;
callback: function;
}
但是第二个没有被接受.
but the 2nd one is not being accepted.
推荐答案
全局类型 Function
用于此目的.
The global type Function
serves this purpose.
此外,如果您打算使用 0 个参数调用此回调并忽略其返回值,则类型 () =>void
匹配所有不带参数的函数.
Additionally, if you intend to invoke this callback with 0 arguments and will ignore its return value, the type () => void
matches all functions taking no arguments.
这篇关于如何为方法参数中使用的函数回调定义类型(作为任何函数类型,而不是通用类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文