How can I create a Typescript Map to store Subject/BehaviorSubject instances of different data types?
I am looking to implement a Map in one of my service classes that can store Subjects of various data types using Typescript. The key for this map will be a number data type. How should I declare and initialize this Map? Below is the example code snippet:
export class EventBusService {
private events: Map<number, Subject<any>>;
constructor() { }
public registerEvent<T>(id: number, initial: T): BehaviorSubject<T> {
if (this.events.has(id)) {
throw new Error('The event id already exists: ' + id);
}
const subject = new BehaviorSubject<T>(initial);
this.events.set(id, subject);
return subject;
}
}