/**
* 使用接口表示函数类型,我们需要给接口定义一个调用签名。
* 一个只有参数列表和返回值类型的函数定义。
*/
interface SearchFunc {
(source: string, substring: string): boolean;
}
let mysearch: SearchFunc;
//函数的参数名不需要与接口里定义的名字相匹配
mysearch = function(sou: string, sub: string): boolean {
return sou.search(sub) > -1;
}
//函数的参数会逐个进行检查,要求对应位置上的参数类型是兼容的。
let res = mysearch('abcd', "abc");
console.log(res);
typescript中使用接口表示函数
最新推荐文章于 2025-06-06 23:55:34 发布