我正尝试在函数的this上添加类型,但我不确定如何执行此操作
interface ThisFunction{
test:Function
}
function exampleFunction(){
//I want to add type of this to be of interface of ThisFunction like
//I know that I will always bind the Test instance this should have interface as ThisFunction
this.test();
}
//When I call the function
exampleFunction().bind({test: ()=>null})
2条答案
按热度按时间4szc88ey1#
您可以按如下方式键入
this
:这段代码是安全的,因为TypeScript静态地检查
this
是否与其声明的类型匹配。例如,如果您试图在不绑定exampleFunction
的情况下调用它,它将抛出一个错误。但是,如果您不想将未绑定的函数作为变量公开,您可以内联函数声明和bind
操作:nwlls2ji2#
要输入
this
,我想您可以这样编写代码: