mouchin777
mouchin777

Reputation: 1588

Why is this typescript function assuming that im passing strings instead of numbers

I have the following function that expects numbers

export const getColor = (open: number, close: number) => {
    if (open - close > 0) {
        return "red";
    } else {
        return "green";
    }
};

So im calling it inside this map

 //Draw the candles
    props.data.map((i: dataObj, index: number) => {
      //Width is accumulated only after the first candle has been drawn
      if (index > 0 && candleWidth) {
        accumulatedWith += candleWidth;
      }
      if (candleWidth) {
      /*To draw the body of the candle*/
      drawCandle(
        context,
        accumulatedWith,
        calcTopBody(
          props.data[index - 1]?.close,
          i.open,
          i.close,
          heightCubicles,
          maxHigh
        ),
        candleWidth,
        Math.abs((i.open - i.close) * heightCubicles),
        getColor(i.open, i.close) //Argument of type 'string' is not assignable to parameter 
        //of type 'number'.
      );

      /*To draw the tail of the candle*/
      
        drawLine(context, accumulatedWith + (candleWidth / 2), (maxHigh - i.high) * heightCubicles, 1, (i.high - i.low) * heightCubicles, getColor(i.open, i.close));
      }

    })

Why is that happening if im typing properly my "i" , and im using numbers for this typing, so all the data that should come from i, should be numbers

type dataObj = {
    low: number,
    high: number,
    open: number,
    close: number,
    index: number
}

Upvotes: 0

Views: 78

Answers (1)

Chris Farmer
Chris Farmer

Reputation: 25415

Is the compiler complaining about the type of the last argument to drawCandle? You're passing it the result of getColor, which is a string. Does the sixth parameter to drawCandle expect a string or a number?

Upvotes: 1

Related Questions