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

问题描述

enter image description here

将我的 react es6 comp 转换为 typescript 时,我在 IDE 的第 16 行 (

类型 '{ data: { labels: string[]; 中缺少属性 'type'数据集:{标签:字符串;数据:字符串[]; }[]; };高度:数量;宽度:数量;选项:{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;

解决方法

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

如果传入一个 type 属性,它将摆脱类型错误,并且没有任何效果(因为 Line 组件 overwrites 是类型属性):

<Line
  type="line"
  data={{
    //etc

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...