目前我有类型定义为:
interface Param {
title: string;
callback: any;
}
我需要这样的东西:
interface Param {
title: string;
callback: function;
}
但是第二项不被接受。
目前我有类型定义为:
interface Param {
title: string;
callback: any;
}
我需要这样的东西:
interface Param {
title: string;
callback: function;
}
但是第二项不被接受。
当前回答
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匹配所有不带参数的函数。
根据Ryan的回答,我认为你所寻找的接口定义如下:
interface Param {
title: string;
callback: () => void;
}
定义函数类型有多种方法;然而,有些比其他的好。
虽然可以使用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
}
我刚开始使用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);
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)结合使用更实用。
总结
函数属性和方法声明之间存在类型差异 如果可能,为较强的类型选择函数属性
操场示例代码