Exploring a new approach has presented me with a minor challenge:
This is what I have=>
export interface SynchGroupSubject {
type: SynchGroupEvent;
target: any;
}
export enum SynchGroupEvent {
ADD_MAP,
REMOVE_MAP
}
Within a service, the following code exists
public createNewSynchGroup(id?: string): Observable<SynchGroupSubject> {
const subject = new Subject<SynchGroupSubject>();
this.synchronizedGroupSubject.set(id, subject);
return subject.asObservable();
}
By subscribing to the update event, I can follow along =>
this.synchObs = this.mapSynchService.createNewSynchGroup(this.synchID);
this.synchObs.subscribe(event => this.synchEventHandler(event));
synchEventHandler(event: SynchGroupSubject) {
switch (event.type) {
case SynchGroupEvent.ADD_MAP:
//event.target is of type any
break;
default:
break;
}
}
The issue lies in the fact that I would like to specify the type of the target so that during the switch statement, I am aware of what I am dealing with. Currently, the target is of type any, but I desire to bind a type relevant to the event type.
One way to achieve this goal would be to create multiple types, as shown below :
export interface ADD_MAP_TYPE {
type: SynchGroupEvent;
target: MyTypedTarger;
}
and then do
synchEventHandler(event: SynchGroupSubject) {
switch (event.type) {
case SynchGroupEvent.ADD_MAP:
event.target = event.target as ADD_MAP_TYPE;
break;
default:
break;
}
However, I prefer something like SynchGroupEventType.ADD_MAP.
I considered using namespaces, but it seems unnecessary.
In addition, if I want to dispatch an event
public dispatchSynchedEvent(id: string, type: SynchGroupEvent, value: any)
{
myElement.next({
type: type,
target: value,
})
}
Even here, my value is of type any, and I wish to be able to do the following
public dispatchSynchedEvent(id: string, type: SynchGroupEvent, value: any)
{
myElement.next({
type: type,
target: value as , // get the type of the value thx to the SynchGroupEvent type
})
}