【发布时间】:2021-12-11 07:26:48
【问题描述】:
在我的 vue 组件中使用带有 lodash debounce 的匿名函数作为 watch 选项时,我收到 typescript 编译器错误“这隐含地具有任何类型。
这是我的组件
export default defineComponent({
data(){
return {
filters:{
filter1: "foo"
} as Record<string, string>
}
},
methods:{
load(){
//load function
}
},
watch:{
"filters":{
deep: true,
handler:_debounce(function(newValue){
this.load() // <= hear I got "this implicitly has type any"
}, 500)
}
}
})
我在这篇文章中发现我的匿名去抖回调函数使用箭头函数,但错误仍然存在 (TS2683 (TS) 'this' implicitly has type 'any' because it does not have a type annotation)
另一种解决方案是将“this:any”作为参数传递给函数,但我发现这很丑。
_debounce(function(this:any, newValue){
this.load() // <= hear I got "this implicitly has type any"
}, 500)
有没有更好的解决方案来处理这个问题? (没有将 noImplicitAny 设置为 false)
【问题讨论】:
标签: typescript vue.js compiler-construction debounce