I'm using the definitely typed package for json-logic-js.
I can't seem to get TypeScript to compile when I'm using a dynamic operator, but it works fine if I use a static one. This works:
const staticRule: RulesLogic = {
'==': [{var:'one'},1]
}
but this doesn't:
const op: ReservedOperations[] = ['==','>=','<=','===']
const dynamicOperator = op[1]
const dynamicRule: RulesLogic = {
[dynamicOperator]: [{var:'one'},1]
}
I get the error:
Type '{ [x: string]: (number | { var: string; })[]; }' is not assignable to type 'RulesLogic<never>'.
Property 'log' is missing in type '{ [x: string]: (number | { var: string; })[]; }' but required in type 'JsonLogicLog<never>'.
Here's a TypeScript playground.
Even if I specify just both those operators it still doesn't work:
const createRulesLogic = (operation: '==' | '!='): RulesLogic => ({
[operation]: [{var:'one'},1]
})
Results in the error:
Type '{ [x: string]: (number | { var: string; })[]; }' is not assignable to type 'RulesLogic<never>'.
Property 'log' is missing in type '{ [x: string]: (number | { var: string; })[]; }' but required in type 'JsonLogicLog<never>'.
staticRule
is automatically recognized as a validRulesLogic
, in particular asJsonLogicEqual
due to the fact that you explicit set the operator/ket to==
.The "dynamic" one doesn't know in advance which operator will you use and warns you that, if the dynamic operator would be a
log
, your rule will be missing thelog
key inside the object.