【问题标题】:Property 'type' is missing in type .but required in type 'Props'类型中缺少属性“类型”。但类型“道具”中是必需的
【发布时间】:2021-07-26 07:58:58
【问题描述】:

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

类型'{数据:{标签:字符串[];中缺少属性'类型';数据集:{标签:字符串;数据:字符串[]; }[]; };高度:数字;宽度:数字;选项:{维护AspectRatio:布尔值;尺度:{ yAxes:{ 刻度:{ beginAtZero:布尔值; }; }[]; };传奇: { ...; }; }; }' 但在“道具”类型中是必需的。

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;

【问题讨论】:

    标签: reactjs typescript react-chartjs-2


    【解决方案1】:

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

    如果传入一个type prop,就会摆脱类型错误,并且没有效果(因为Line组件overwrites类型prop):

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

    【讨论】:

    • 谢谢!我同意这个图表库在与 Typescript 一起使用时还有其他问题,我无法从 lib 中解构 fromDate 和 toDate 道具
    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2020-12-25
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多