Below is the code that I have:
class Eventful<T extends string> {
// ↓ How can I initialize this attribute without TypeScript error?
private eventMap: Record<T, (args?: any) => void> = ?
}
Alternatively,
class Eventful<T extends string> {
private eventMap!: Record<T, (args?: any) => void>
constructor() {
this.eventMap = ?
}
}
What should I do in this case? Thank you.
I attempted to initialize the attribute with:
function addEvent(key: T, handler: (args?: any) => void>) {
this.eventMap = {
[key]: [] as EventHandler[]
}
}
However, this also results in a TypeScript error (ts(2322)) in Visual Studio Code.