目前我有类型定义为:

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

我需要这样的东西:

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

但是第二项不被接受。


当前回答

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

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

其他回答

有四种抽象函数类型,当你知道你的函数是否接受参数,是否返回数据时,你可以分别使用它们。

export declare type fEmptyVoid = () => void;
export declare type fEmptyReturn = () => any;
export declare type fArgVoid = (...args: any[]) => void;
export declare type fArgReturn = (...args: any[]) => any;

是这样的:

public isValid: fEmptyReturn = (): boolean => true;
public setStatus: fArgVoid = (status: boolean): void => this.status = status;

为了只使用一种类型作为任何函数类型,我们可以将所有抽象类型组合在一起,如下所示:

export declare type fFunction = fEmptyVoid | fEmptyReturn | fArgVoid | fArgReturn;

然后这样使用它:

public isValid: fFunction = (): boolean => true;
public setStatus: fFunction = (status: boolean): void => this.status = status;

在上面的例子中,一切都是正确的。但是从大多数代码编辑器的角度来看,下面的使用示例是不正确的。

// you can call this function with any type of function as argument
public callArgument(callback: fFunction) {

    // but you will get editor error if call callback argument like this
    callback();
}

正确的编辑召唤是这样的:

public callArgument(callback: fFunction) {

    // pay attention in this part, for fix editor(s) error
    (callback as fFunction)();
}

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)结合使用更实用。

总结

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

操场示例代码

全局类型Function用于此目的。

此外,如果你打算用0个参数调用这个回调函数并忽略它的返回值,type () => void匹配所有不带参数的函数。

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

如果你也想使用args,

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

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

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