在TypeScript中,我可以将函数的参数声明为Function类型.是否存在一种"类型安全"的方式来解决这个问题?例如,考虑一下:
class Foo {
save(callback: Function) : void {
//Do the save
var result : number = 42; //We get a number from the save operation
//Can I at compile-time ensure the callback accepts a single parameter of type number somehow?
callback(result);
}
}
var foo = new Foo();
var callback = (result: string) : void => {
alert(result);
}
foo.save(callback);
Run Code Online (Sandbox Code Playgroud)
保存回调不是类型安全的,我给它一个回调函数,其中函数的参数是一个字符串,但我传递一个数字,并编译没有错误.我可以在保存类型安全功能时创建结果参数吗?
TL; DR版本:在TypeScript中是否有等效的.NET委托?
typescript ×1