目前我有类型定义为:
interface Param {
title: string;
callback: any;
}
我需要这样的东西:
interface Param {
title: string;
callback: function;
}
但是第二项不被接受。
目前我有类型定义为:
interface Param {
title: string;
callback: any;
}
我需要这样的东西:
interface Param {
title: string;
callback: function;
}
但是第二项不被接受。
当前回答
有四种抽象函数类型,当你知道你的函数是否接受参数,是否返回数据时,你可以分别使用它们。
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)();
}
其他回答
下面是一个接受回调函数的例子
const sqk = (x: number, callback: ((_: number) => number)): number => {
// callback will receive a number and expected to return a number
return callback (x * x);
}
// here our callback will receive a number
sqk(5, function(x) {
console.log(x); // 25
return x; // we must return a number here
});
如果你不关心回调函数的返回值(大多数人不知道如何有效地利用它们),你可以使用void
const sqk = (x: number, callback: ((_: number) => void)): void => {
// callback will receive a number, we don't care what it returns
callback (x * x);
}
// here our callback will receive a number
sqk(5, function(x) {
console.log(x); // 25
// void
});
注意,我用于回调参数的签名…
const sqk = (x: number, callback: ((_: number) => number)): number
我会说这是TypeScript的缺陷,因为我们希望为回调参数提供一个名称。在本例中,我使用_,因为它在sqk函数中不可用。
然而,如果你这样做
// danger!! don't do this
const sqk = (x: number, callback: ((number) => number)): number
它是有效的TypeScript,但是它会被解释为…
// watch out! typescript will think it means ...
const sqk = (x: number, callback: ((number: any) => number)): number
也就是说,TypeScript会认为参数名是number,隐含的类型是any。这显然不是我们想要的,但是,这就是TypeScript的工作方式。
因此,在输入函数参数时,不要忘记提供参数名……尽管这看起来很愚蠢。
全局类型Function用于此目的。
此外,如果你打算用0个参数调用这个回调函数并忽略它的返回值,type () => void匹配所有不带参数的函数。
希望这对你有所帮助……
interface Param {
title: string;
callback: (error: Error, data: string) => void;
}
或在函数中
let myfunction = (title: string, callback: (error: Error, data: string) => void): string => {
callback(new Error(`Error Message Here.`), "This is callback data.");
return title;
}
定义函数类型有多种方法;然而,有些比其他的好。
虽然可以使用JavaScript函数对象Function,但不要这样做。 ESLint插件推荐规则ban-types
避免使用Function类型,因为它提供的安全性很少,原因如下: 它在调用值时不提供类型安全,这意味着很容易提供错误的参数。 它接受类声明,类声明在调用时会失败,因为调用时没有new关键字。
TypeScript支持多种其他方式。最常见的是使用函数类型表达式。这个方法非常类似于箭头函数。
如果已知参数和返回值是某种形式,则应该键入它们。 例如,
interface Param {
callback: (foo: string, bar: number) => void
}
请注意,这些类型可以根据需要非常复杂,例如使用对象类型或从其他类型创建的类型。
如果类型确实是未知的,则选择unknown而不是any。 ESLint插件推荐规则no-explicit-any
当使用any时,所有围绕该值的编译器类型检查都将被忽略。
从TS文档中,
Unknown是any的类型安全对应项。
因此,使用扩展语法,
interface Params {
callback: (...args: unknown[]) => unknown
}
如果您正在寻找类似于change事件的输入回调函数,则使用以下方法
type Props = {
callBack: ChangeEventHandler<HTMLInputElement>;
}