How can we modify example 1 to function without requiring the passing of <UserDomainEvent>
like in example 2?
The goal is to simplify usage.
abstract class DomainEvent {
static on1(listener: (e: DomainEvent) => void): void { }
static on2(listener: <T extends DomainEvent>(e: T) => void): void { }
}
export class UserDomainEvent extends DomainEvent {
public readonly userId: number = 1;
}
// Usage
// Is there a way to make this work,
// without the need to pass type specifics as in example 2?
const handler = (e: UserDomainEvent): void => { }
DomainEvent.on1(handler) // error
const handler2 = <UserDomainEvent>(e: UserDomainEvent): void => { }
DomainEvent.on2(handler2)