0

I'm playing around with JsonLogic and more precisely json-logic-js.

I have a very basic nodejs project with typescript setup. When I now enter seemingly the same code, one version passes compilation, the other one throws an error. The code is taken from the official website of the JsonLogic project.

working version:

import * as jsonLogic from 'json-logic-js';

console.log(
  jsonLogic.apply({ and: [{ '>': [3, 1] }, { '<': [1, 3] }] })
);

not working version:

import * as jsonLogic from 'json-logic-js';

const jsonLogic = { and: [{ '>': [3, 1] }, { '<': [1, 3] }] };
console.log(
  jsonLogic.apply(jsonLogic)
);

The "not working version" already shows me an error in vscode, and does not compile. The error is:

error TS2345: Argument of type '{ and: ({ '>': number[]; '<'?: undefined; } | { '<': number[]; '>'?: undefined; })[]; }' is not assignable to parameter of type 'RulesLogic<AdditionalOperation>'.
  Type '{ and: ({ '>': number[]; '<'?: undefined; } | { '<': number[]; '>'?: undefined; })[]; }' is not assignable to type 'JsonLogicAnd<AdditionalOperation>'.
    Types of property 'and' are incompatible.
      Type '({ '>': number[]; '<'?: undefined; } | { '<': number[]; '>'?: undefined; })[]' is not assignable to type 'RulesLogic<AdditionalOperation>[]'.
        Type '{ '>': number[]; '<'?: undefined; } | { '<': number[]; '>'?: undefined; }' is not assignable to type 'RulesLogic<AdditionalOperation>'.
          Type '{ '>': number[]; '<'?: undefined; }' is not assignable to type 'RulesLogic<AdditionalOperation>'.
            Type '{ '>': number[]; '<'?: undefined; }' is not assignable to type 'AdditionalOperation'.
              Type '{ '>': number[]; '<'?: undefined; }' is not assignable to type 'Partial<Record<ReservedOperations, never>>'.
                Types of property '">"' are incompatible.
                  Type 'number[]' is not assignable to type 'undefined'.

Can anybody explain why the two versions of the code are interpreted by the compiler differently in the first place and maybe even provide a fix?

kelsny
  • 23,009
  • 3
  • 19
  • 48
tommueller
  • 2,358
  • 2
  • 32
  • 44

0 Answers0