Home > Enterprise >  React Apexcharts not working with Typescript
React Apexcharts not working with Typescript

Time:09-22

I'm trying to implement a simple ApexChart on a React/Typescript project, however, I'm stuck on the following type error:

No overload matches this call.
  Overload 1 of 2, '(props: Props | Readonly<Props>): ReactApexChart', gave the following error.
    Type '{ chart: { height: ApexOptions; type: ApexOptions; zoom: { enabled: ApexOptions; }; }; }' is not assignable to type 'ApexOptions'.
      The types of 'chart.height' are incompatible between these types.
        Type 'ApexOptions' is not assignable to type 'string | number | undefined'.
          Type 'ApexOptions' is not assignable to type 'number'.
  Overload 2 of 2, '(props: Props, context: any): ReactApexChart', gave the following error.
    Type '{ chart: { height: ApexOptions; type: ApexOptions; zoom: { enabled: ApexOptions; }; }; }' is not assignable to type 'ApexOptions'.

I did install both react-apexcharts and apexcharts, as usual. Here's the script of my chart:

**TasksChart.tsx**


import ReactApexChart from 'react-apexcharts';

const TasksChart: React.FC<Props> = ({
tasks,
}) => {

  const options = {
    chart: {
      height: 350,
      type: 'line',
      zoom: {
        enabled: true
      }
    },
  };

  series = [{
    name: 'All Tasks',
    data: [31, 40, 28, 51, 42, 109, 100]
  }, {
    name: 'My Tasks',
    data: [11, 32, 45, 32, 34, 52, 41]
  }]

  return (
    <>
      <ReactApexChart options={options} series={series} type="line" height={350} />
    </>
  );
};

export default TasksChart;

Any thoughts on how to fix this issue? Thanks!

CodePudding user response:

Looks like you've already set the type property as the <ReactApexChart /> prop.

Try to remove the type from the chart in options and the error is gone:

const TasksChart: React.FC<Props> = ({ tasks }) => {
  const options = {
    chart: {
      height: 350,
      zoom: {
        enabled: true
      }
    }
  };

  const series = [
    {
      name: "All Tasks",
      data: [31, 40, 28, 51, 42, 109, 100]
    },
    {
      name: "My Tasks",
      data: [11, 32, 45, 32, 34, 52, 41]
    }
  ];

  return (
      <ReactApexChart
        type="line"
        options={options}
        series={series}
        height={350}
      />
  );
};
  • Related