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

plugins.legend.align are incompatible types typescript chartjs

我的选项对象中有这样的东西

const options = {
  plugins: {
    legend: {
      align: 'end',
    }
  }
}

根据 Chartjs documentation,对齐接受 3 个值:

对齐工作正常,我得到了想要的结果,但打字稿抱怨对齐类型应该是 'start'、'center' 或 'end' 而不是字符串。

Type 'string' is not assignable to type ' "end" | "start" | "center" | undefined '

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

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

}

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

非常感谢任何想法/评论/建议,谢谢!

问题似乎在 react-chartjs-2 库中。

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