简体   繁体   中英

Property 'property' does not exist on type 'IntrinsicAttributes'

I'm a beginner to React Typescript. I'm currently studying How to implement a Bar chart using Chart.js in React Typescript App. So I just want to pass values of the property datasets: as a props to the BarChart.tsx component.

So here is my BarChart.tsx component:

import Chart from 'chart.js/auto';
import { Line } from 'react-chartjs-2';
import 'chartjs-adapter-luxon';
import StreamingPlugin from 'chartjs-plugin-streaming';

Chart.register(StreamingPlugin);

interface ChartProperty {
  label: string;
  backgroundColor: string;
  borderColor: string;
  borderDash?: number[];
  cubicInterpolationMode?: string;
  fill: boolean;
  data: [];
}

interface Props {
  property: ChartProperty[];
}

const BarChart: React.FC<Props> = ({property}) => {
  return (
    <div>
      <Line
        data={{
          datasets: property,
        }}
        height={100}
        width={400}
        options={{
          scales: {
            x: {
              type: 'realtime',
              realtime: {
                delay: 2000,
                onRefresh: (chart) => {
                  chart.data.datasets.forEach((dataset) => {
                    dataset.data.push({
                      x: Date.now(),
                      y: Math.random(),
                    });
                  });
                },
              },
            },
          },
        }}
      />
    </div>
  );
};

export default BarChart;

And here is my App.tsx component:

import './App.css';
import BarChart from './components/BarChart';

const ChartProperty = [
  {
    label: 'Dataset 1',
    backgroundColor: 'rgba(255, 99, 132, 0.5)',
    borderColor: 'rgb(255, 99, 132)',
    borderDash: [8, 4],
    fill: false,
    data: [],
  },
  {
    label: 'Dataset 2',
    backgroundColor: 'rgba(54, 162, 235, 0.5)',
    borderColor: 'rgb(54, 162, 235)',
    cubicInterpolationMode: 'monotone',
    fill: false,
    data: [],
  },
];

const App = () => {
  return (
    <div>
      <h1 style={{ textAlign: 'center' }}>Sample Bar Chart</h1>
      <BarChart property={ChartProperty} />
    </div>
  );
};

export default App;

But here when I'm trying this I'm getting error in <BarChart property={ChartProperty} /> in prop named property . So here is the error I'm getting:

TS2322: Type '{ property: ({ label: string; backgroundColor: string; borderColor: string; borderDash: number[]; fill: boolean; data: never[]; cubicInterpolationMode?: undefined; } | { label: string; backgroundColor: string; ... 4 more ...; borderDash?: undefined; })[]; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'property' does not exist on type 'IntrinsicAttributes'.
    25 |     <div>
    26 |       <h1 style={{ textAlign: 'center' }}>Sample Bar Chart</h1>        
  > 27 |       <BarChart property={ChartProperty} />
       |                 ^^^^^^^^
    28 |     </div>
    29 |   );
    30 | };

Can someone help me in this case? I'm really appreciate if someone can show my error.Thank you in advance.

You can't write like this: data: []; . Correct: data: any[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM