0

I got the implicitly 'any type' with React Typescript when building a generate data function (for the chart). So, what type it should be? My code is:

interface dataType {
  label: string;
  topic: {
    A: number[];
    B?: number[];
    C?: number[];
  };
}

const generateData = ({ inputData }:dataType) => {
  return {
    labels: inputData.label,
    datasets: [
      {
        label: "A",
        data: inputData.topic.A,
        borderColor: "black",
        backgroundColor: "blue",
      },
      {
        label: "B",
        data: inputData.topic.B,
        borderColor: "black",
        backgroundColor: "blue",
      },
      {
        label: "C",
        data: inputData.topic.C,
       borderColor: "black",
        backgroundColor: "blue",
      },
    ],
  };
};

Thank you in advanced

Banus
  • 79
  • 1
  • 8
  • Where is param in your code? – Tushar Shahi Dec 30 '21 at 11:00
  • const generateData = ({ data }:dataType) => { here you are trying to destructure the parameter, which is wrong. Replace this line with this: const generateData = (data: dataType) => { } And also, it is a good practice to name interfaces, types ect... with a capital letter, like DataType. – Tigran Petrosyan Dec 30 '21 at 11:08
  • @TusharShahi I creating the function, the data has not been set up yet, but I dont know why it yells the type error. – Banus Dec 30 '21 at 13:59
  • @TigranPetrosyan Oh, I change as you said and it works. Also, can you explain to me why? Because as usual, I destruct the props like const Func ({ prop }} but in this case it just needs const Func (prop). – Banus Dec 30 '21 at 14:08
  • Here you are passing a parameter to a function, which you are creating. We destructure(pull out) values from array or object. – Tigran Petrosyan Dec 30 '21 at 15:56
  • @TigranPetrosyan I got it, thank you !! – Banus Dec 31 '21 at 04:19

0 Answers0