Open comp615 opened 11 months ago
FWIW I've used type-safe facts in the past using the following code (c/p from an old project):
declare module "json-rules-engine" {
interface Engine {
facts: Map<keyof Facts, Fact>;
sfv<T extends FactId>(id: T, value: Facts[T]): void;
gfv<T extends FactId>(id: T): Facts[T];
}
}
export type Facts = {
"my:version": string;
"flow:step": number;
}
And then, using the following two methods for storage/retrieval:
Engine.prototype.sfv = function <T extends FactId>(k: T, v: Facts[T]): void {
this.removeFact(k);
this.addFact(new Fact(k, v));
};
and
Engine.prototype.gfv = function <T extends FactId>(k: T): Facts[T] {
return this.getFact(k)?.value as Facts[T];
};
This helped me avoid the typos in facts around the code and also have type safety.
Edit
Forgot to add the FactId
type:
export type FactId = keyof Facts;
We're doing a quick POC and looking at using this library. One thing that I'd love to see is the ability to enforce or at least make consistent the concept of fact types. Something along the lines of:
You get the idea. Let users specify a dictionary of facts whose type is known ahead of time and fixed. Today, this is kind of done by just letting the user set the type of the fact value, but it doesn't carry through the system.
I took an initial stab at doing this which is below, but the issue is that this would require a breaking type change.
Today since users can specify the type
almanac.factValue<number>('userId')
. But in order to infer types, we'd need to template the key-type as well, giving us two generics. TS doesn't like having partially filled out generics...so we hit an impasse: if we want to allow user overriding of the types, then we'll need to ask people to specify<number, string>
where before it was just<number>
.Of course the ideal scenario is that they remove the template together and just pass a dictionary of types on engine construction; changing this usage would be smoothest by default, but they could still override the type by casting through any (or we could make any the default instead of unknown).
Here's the new ts file, complete with changes if anyone would like to play around further or discuss more.
View Code
```typescript export interface EngineOptions { allowUndefinedFacts?: boolean; allowUndefinedConditions?: boolean; pathResolver?: PathResolver; } export interface EngineResult