在javascript中,我们可以声明有关键值类型的数组
let a = new Array
a['name'] = function(){}
但是在Typescript中,应该怎么做
type EventFunc = (...args: any[]) => any
interface CallbackBox {
[key:string]: EventFunc
}
class EventProxy {
private _callbacks:CallbackBox[] = []
addEventListener(ev: string, callback: EventFunc): EventProxy {
# **error type string but use any it can pass compile**
this._callbacks[ev] = this._callbacks[ev] || []
this._callbacks[ev].push(callback)
return this
}
}