在严格模式下处理 inner this inside 回调函数

Handle inner this inside callback function in strict mode

我在严格模式下使用 webix(5.4.0) + typescript(3.1.1) 时遇到问题 TS2683。

($$('DATE') as webix.ui.datepicker)attachEvent('onChange', function() {
      let val:any = this.getValue() // or let val = ... without any
      // this:this??? () = > not suit for me, btw
      ...
}

错误是:

'this' implicity has type 'any' because it doesn't have a type annotation.

我看过了How to access the correct `this` inside a callback?但是看起来,一切都是关于处理outer this,而且我需要声明inner this类型。那我该怎么办呢?

需要添加类型注解让编译器知道this

的类型
($$('DATE') as webix.ui.datepicker).attachEvent('onChange', function(this:webix.ui.datepicker) {
    let val:any = this.getValue()    
});

理想情况下,attachEvent 方法应该在回调类型中指定 this 的类型,但由于它不需要您手动指定。