Skip to content

Commit 5a6deec

Browse files
committed
updated on method overload to include generic method call
1 parent d20bd68 commit 5a6deec

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

types/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Engine {
5353

5454
on(eventName: "success", handler: EventHandler): this;
5555
on(eventName: "failure", handler: EventHandler): this;
56-
on(eventName: string, handler: EventHandler): this;
56+
on<T>(eventName: string, handler: EventHandler<T>): this;
5757

5858
run(facts?: Record<string, any>, runOptions?: RunOptions): Promise<EngineResult>;
5959
stop(): this;
@@ -119,8 +119,8 @@ export interface Event {
119119

120120
export type PathResolver = (value: object, path: string) => any;
121121

122-
export type EventHandler = (
123-
event: Event,
122+
export type EventHandler<T = Event> = (
123+
event: T,
124124
almanac: Almanac,
125125
ruleResult: RuleResult
126126
) => void;

types/index.test-d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import rulesEngine, {
1010
PathResolver,
1111
Rule,
1212
RuleProperties,
13-
RuleSerializable
13+
RuleSerializable,
14+
Event
1415
} from "../";
1516

1617
// setup basic fixture data
@@ -93,6 +94,15 @@ expectType<Engine>(engine.addFact(fact));
9394
expectType<Engine>(engine.addFact(dynamicFact));
9495
expectType<boolean>(engine.removeFact(fact));
9596
expectType<Fact<string>>(engine.getFact<string>("test"));
97+
engine.on('success', (event) => {
98+
expectType<Event>(event)
99+
})
100+
engine.on('failure', (event) => {
101+
expectType<Event>(event)
102+
})
103+
engine.on<{ foo: Array<string> }>('foo', (event) => {
104+
expectType<{ foo: Array<string> }>(event)
105+
})
96106

97107
// Run the Engine
98108
expectType<Promise<EngineResult>>(engine.run({ displayMessage: true }));

0 commit comments

Comments
 (0)