plugins.legend.align 是不兼容的类型 typescript Chartjs

Jos*_*sho 5 typescript chart.js

我的选项对象中有类似的东西

const options = {
  plugins: {
    legend: {
      align: 'end',
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

根据 Chartjs文档,align 接受 3 个值:

  • 开始
  • 中心
  • 结尾

对齐工作正常,我得到了想要的结果,但打字稿抱怨对齐的类型应该是“开始”、“中心”或“结束”而不是字符串。

Type 'string' is not assignable to type ' "end" | "start" | "center" | undefined '
Run Code Online (Sandbox Code Playgroud)

深入研究 Chartjs 类型定义,我明白了

export interface LegendOptions<TType extends ChartType> {
  ....
  /**
   * Alignment of the legend.
   * @default 'center'
   */
  align: 'start' | 'center' | 'end';

}
Run Code Online (Sandbox Code Playgroud)

如果我将对齐更改为字符串类型。打字稿错误消失了,但我不应该这样做。

非常感谢任何想法/意见/建议,谢谢!

Lee*_*lee 2

问题似乎出在库内react-chartjs-2

如果您切换到 Chart.js 的准系统版本,则不会遇到此问题:https://codesandbox.io/s/goofy-curie-eyxrr ?file=/src/App.tsx