Skip to content

Commit c688692

Browse files
begin to add condition result types
1 parent 7a90a81 commit c688692

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

types/index.d.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export type RuleResultSerializable = Pick<
159159

160160
export interface RuleResult {
161161
name: string;
162-
conditions: TopLevelCondition;
162+
conditions: TopLevelConditionResult;
163163
event?: Event;
164164
priority?: number;
165165
result: any;
@@ -184,6 +184,14 @@ export class Rule implements RuleProperties {
184184
): T extends true ? string : RuleSerializable;
185185
}
186186

187+
interface BooleanConditionResultProperties {
188+
result: boolean
189+
}
190+
191+
interface ConditionResultProperties extends BooleanConditionResultProperties {
192+
factResult: any
193+
}
194+
187195
interface ConditionProperties {
188196
fact: string;
189197
operator: string;
@@ -194,25 +202,41 @@ interface ConditionProperties {
194202
name?: string;
195203
}
196204

205+
type ConditionPropertiesResult = ConditionProperties & ConditionResultProperties
206+
197207
type NestedCondition = ConditionProperties | TopLevelCondition;
208+
type NestedConditionResult = ConditionPropertiesResult | TopLevelConditionResult;
198209
type AllConditions = {
199210
all: NestedCondition[];
200211
name?: string;
201212
priority?: number;
202213
};
214+
type AllConditionsResult = AllConditions & {
215+
all: NestedConditionResult[]
216+
} & BooleanConditionResultProperties
203217
type AnyConditions = {
204218
any: NestedCondition[];
205219
name?: string;
206220
priority?: number;
207221
};
222+
type AnyConditionsResult = AnyConditions & {
223+
any: NestedConditionResult[]
224+
} & BooleanConditionResultProperties
208225
type NotConditions = { not: NestedCondition; name?: string; priority?: number };
226+
type NotConditionsResult = NotConditions & {not: NestedConditionResult} & BooleanConditionResultProperties;
209227
type ConditionReference = {
210228
condition: string;
211229
name?: string;
212230
priority?: number;
213231
};
232+
type ConditionReferenceResult = ConditionReference & ConditionResultProperties;
214233
export type TopLevelCondition =
215234
| AllConditions
216235
| AnyConditions
217236
| NotConditions
218237
| ConditionReference;
238+
export type TopLevelConditionResult =
239+
| AllConditionsResult
240+
| AnyConditionsResult
241+
| NotConditionsResult
242+
| ConditionReferenceResult

types/index.test-d.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import rulesEngine, {
1414
Rule,
1515
RuleProperties,
1616
RuleResult,
17-
RuleSerializable
17+
RuleSerializable,
18+
TopLevelConditionResult,
19+
AnyConditionsResult,
20+
AllConditionsResult,
21+
NotConditionsResult
1822
} from "../";
1923

2024
// setup basic fixture data
@@ -126,7 +130,20 @@ engine.on<{ foo: Array<string> }>('foo', (event, almanac, ruleResult) => {
126130
})
127131

128132
// Run the Engine
129-
expectType<Promise<EngineResult>>(engine.run({ displayMessage: true }));
133+
const result = engine.run({ displayMessage: true })
134+
expectType<Promise<EngineResult>>(result);
135+
136+
const topLevelConditionResult = result.then(r => r.results[0].conditions);
137+
expectType<Promise<TopLevelConditionResult>>(topLevelConditionResult)
138+
139+
const topLevelAnyConditionsResult = topLevelConditionResult.then(r => (r as AnyConditionsResult).result);
140+
expectType<Promise<boolean>>(topLevelAnyConditionsResult)
141+
142+
const topLevelAllConditionsResult = topLevelConditionResult.then(r => (r as AllConditionsResult).result);
143+
expectType<Promise<boolean>>(topLevelAllConditionsResult)
144+
145+
const topLevelNotConditionsResult = topLevelConditionResult.then(r => (r as NotConditionsResult).result);
146+
expectType<Promise<boolean>>(topLevelNotConditionsResult)
130147

131148
// Alamanac tests
132149
const almanac: Almanac = (await engine.run()).almanac;

0 commit comments

Comments
 (0)