类型中缺少属性“类型”。但类型“道具”中需要

Myt*_*lls 1 typescript reactjs react-chartjs-2

在此处输入图片说明将我的 react es6 comp 转换为打字稿时,我在第 16 行(<Line ...)的 IDE 中遇到了这个奇怪的打字稿错误。它是我使用 react-chartjs-2 的图表组件。

类型 '{ 数据:{ 标签:字符串 []; 中缺少属性 '类型' 数据集:{标签:字符串;数据:字符串[];}[]; }; 高度:数量;宽度:数量;选项:{maintainAspectRatio:布尔值;比例:{ yAxes:{ 刻度:{ beginAtZero:布尔值;}; }[]; }; 传奇: { ...; }; }; }' 但在'Props'类型中是必需的。

import React from "react";
import { Line, defaults } from "react-chartjs-2";

defaults.plugins.tooltip.enabled = true;
defaults.plugins.legend.position = "bottom";

interface Props {
  currencyValues: string[],
  dateList: string[],
}

const LineGraph = ({ currencyValues, dateList }: Props) => {
  return (
    <div className="graphContainer">
      {currencyValues.length ? (
        <Line
          data={{
            labels: [...dateList],
            datasets: [
              {
                label: "Historical Dates",
                data: [...currencyValues],
              },
            ],
          }}
          height={400}
          width={600}
          options={{
            maintainAspectRatio: false,
            scales: {
              yAxes: [
                {
                  ticks: {
                    beginAtZero: true,
                  },
                },
              ],
            },
            legend: {
              labels: {
                fontSize: 25,
              },
            },
          }}
        />
      ) : null}
    </div>
  );
};

export default LineGraph;

Run Code Online (Sandbox Code Playgroud)

Nic*_*wer 6

看起来类型定义说type道具是强制性的。这似乎是库作者的一个错误,在 github ( 1 , 2 )上有多个未解决的问题

如果传入一个typeprop,它会去掉类型错误,并且没有任何作用(因为 Line 组件覆盖了 type prop):

<Line
  type="line"
  data={{
    //etc
Run Code Online (Sandbox Code Playgroud)