bylexus / fparse

A JavaScript Formula Parser
http://fparser.alexi.ch/
MIT License
90 stars 15 forks source link

Multiple types returns #54

Closed boskiv closed 11 months ago

boskiv commented 11 months ago

I have a typescript error with that code (from v2) after upgrading to v3. Is there correct migration procedure?

const time = entry[i][0];
      const open = calculator.evaluate(paramsOpen);
      const high = calculator.evaluate(paramsHigh);
      const low = calculator.evaluate(paramsLow);
      const close = calculator.evaluate(paramsClose);

      kData.push([
        time,
        String(open),
        String(open >= close ? Math.max(high, open) : Math.max(high, close)),
        String(open >= close ? Math.min(low, close) : Math.min(low, open)),
        String(close),
        String(volume),
      ]);
 Argument of type 'number | number[]' is not assignable to parameter of type 'number'.

UPD. At this moment I made a cast workaround

const close = formula.evaluate(params.close) as number;
const open = formula.evaluate(params.open) as number;
const high = formula.evaluate(params.high) as number;
const low = formula.evaluate(params.low) as number;
const volume = formula.evaluate(params.volume) as number;
bylexus commented 11 months ago

Hi,

Thanks for pointing that out, and sorry for the inconvenience. Before the v3 version, this lib was plain JavaScript without TypeScript types. As of 3.0, I migrated to TypeScript, which now (correctly) defines the return value of evaluate() as number|number[].

The reason for this is that you can call evaluate() with an array of input values, instead of a single value: by doing so, evaluate also returns an array of results, one for each input.

So the return type is correct, but was not defined before. Hope you can live with that, your workaround is a reasonable way to go in this case.