关于数组声明的打字稿

时间:2018-12-23 04:53:20

标签: javascript typescript

在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
    }  
}

1 个答案:

答案 0 :(得分:0)

您要寻找的内容称为打字稿中的索引签名。 Here是有关此的文章。它如何帮助