[英]Typescript about array declare
在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
}
}
您所寻找的就是打字稿中的索引签名。 这是一篇关于它的文章。 它如何帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.