目前我有类型定义为:

interface Param {
    title: string;
    callback: any;
}

我需要这样的东西:

interface Param {
    title: string;
    callback: function;
}

但是第二项不被接受。


当前回答

在typescript 4.8中,函数类型给出错误。相反,我们可以显式地将类型写为fn: () => void。

如果你也想使用args,

函数debounce(fn:(…参数:any[]) => void, ms = 300) {

其他回答

根据Ryan的回答,我认为你所寻找的接口定义如下:

interface Param {
    title: string;
    callback: () => void;
}

Typescript:如何为方法参数中使用的函数回调定义类型?

你可以声明回调为1)函数属性或2)方法:

interface ParamFnProp {
    callback: (a: Animal) => void; // function property
}

interface ParamMethod {
    callback(a: Animal): void; // method
}

自TS 2.6以来,有一个重要的类型差异:

当声明函数属性时,在——strict或——strictFunctionTypes模式下可以获得更强的(“sound”)类型。让我们举个例子:

const animalCallback = (a: Animal): void => { } // Animal is the base type for Dog
const dogCallback = (d: Dog): void => { } 
// function property variant
const param11: ParamFnProp = { callback: dogCallback } // error: not assignable
const param12: ParamFnProp = { callback: animalCallback } // works

// method variant
const param2: ParamMethod = { callback: dogCallback } // now it works again ...

从技术上讲,在strictFunctionTypes下,方法是双变的,函数属性在参数中是逆变的。方法的检查仍然更加允许(即使不是sound),以便与内置类型(如Array)结合使用更实用。

总结

函数属性和方法声明之间存在类型差异 如果可能,为较强的类型选择函数属性

操场示例代码

从v1.4开始的Typescript有type关键字,它声明了一个类型别名(类似于C/ c++中的typedef)。你可以这样声明你的回调类型:

type CallbackFunction = () => void;

它声明了一个不接受参数且不返回任何值的函数。接受零个或多个任意类型参数且不返回任何值的函数是:

type CallbackFunctionVariadic = (...args: any[]) => void;

然后你可以说,例如,

let callback: CallbackFunctionVariadic = function(...args: any[]) {
  // do some stuff
};

如果你想要一个函数接受任意数量的参数并返回任何值(包括void):

type CallbackFunctionVariadicAnyReturn = (...args: any[]) => any;

你可以指定一些强制性参数,然后是一组额外的参数(比如一个字符串,一个数字,然后是一组额外的参数),这样:

type CallbackFunctionSomeVariadic =
  (arg1: string, arg2: number, ...args: any[]) => void;

这对于EventEmitter处理器之类的东西很有用。

在这种方式下,函数的类型可以随心所欲,尽管如果您试图用类型别名确定所有内容,可能会失去控制并遇到组合问题。

你可以用多种方式在接口中定义函数类型,

一般方法:

export interface IParam {
  title: string;
  callback(arg1: number, arg2: number): number;
}

如果你想使用属性语法,

export interface IParam {
  title: string;
  callback: (arg1: number, arg2: number) => number;
}

如果先声明函数类型,

type MyFnType = (arg1: number, arg2: number) => number;

export interface IParam {
  title: string;
  callback: MyFnType;
}

使用非常直接,

function callingFn(paramInfo: IParam):number {
    let needToCall = true;
    let result = 0;
   if(needToCall){
     result = paramInfo.callback(1,2);
    }

    return result;
}

你也可以声明一个函数类型文字,这意味着一个函数可以接受另一个函数作为它的形参。参数化函数也可以作为回调函数调用。

export interface IParam{
  title: string;
  callback(lateCallFn?:
             (arg1:number,arg2:number)=>number):number;

}

我刚开始使用Typescript,我一直试图解决类似的问题,就像这样;如何告诉Typescript我正在传递一个没有接口的回调。

在浏览了Stack Overflow和GitHub问题的一些答案后,我终于找到了一个解决方案,可以帮助任何有同样问题的人。

函数的类型可以用(arg0: type0) => returnType定义,并且可以在另一个函数的形参列表中使用此类型定义。

function runCallback(callback: (sum: number) => void, a: number, b: number): void {
    callback(a + b);
}

// Another way of writing the function would be:
// let logSum: (sum: number) => void = function(sum: number): void {
//     console.log(sum);
// };
function logSum(sum: number): void {
    console.log(`The sum is ${sum}.`);
}

runCallback(logSum, 2, 2);