React Chart.JS 无法移动图例(TypeScript)

Jor*_*ejo 4 typescript reactjs chart.js react-chartjs-2

我有一个包含以下数据的圆环图组件:

  const data = {
    labels: ["1", "2"],
    datasets: [
      {
        label: "peers",
        data: [1, 2],
        backgroundColor: ["#56E2CF", "#56AEE2"]
      }
    ]
  };
Run Code Online (Sandbox Code Playgroud)

数据运行正常。这些是选项:

  const options = {
    legend: {
      display: "right"
    }
  };
Run Code Online (Sandbox Code Playgroud)

甜甜圈成分:

<Doughnut data={data} options={options} />
Run Code Online (Sandbox Code Playgroud)

但图表仍然没有标题: 在此输入图像描述


我尝试使用插件将选项放入 char.js 样式:

  const options = {
    plugins: {
      legend: {
        position: "right"
      }
    }
  };
Run Code Online (Sandbox Code Playgroud)

图例移动了,但出现类型错误(仍然没有标题):The types of 'plugins.legend.position' are incompatible between these types. Type 'string' is not assignable to type '"right" | "left" | "top" | "bottom" | "center" | "chartArea" | _DeepPartialObject<{ [scaleId: string]: number; }> | undefined'

请注意标题如何移至右侧: 在此输入图像描述

Ole*_*leg 8

 const options = {
    plugins: {
      legend: {
        position: "right" as const
      },
      title: {
        display: true,
        text: "Title"
      }
    }
  };
Run Code Online (Sandbox Code Playgroud)

以下是 TypeScript 文档的解释 - https://devblogs.microsoft.com/typescript/announcing-typescript-3-4-rc/#const-assertions