Chart.js设置Y轴标题触发TypeScript编译错误,寻求非断言解法
问题:Chart.js + TypeScript 中设置Y轴标题无需重复类型断言的方案?
从后端订阅数据填充图表后,尝试通过以下代码设置Y轴标题显示:
this.chart.config.options.scales.y.title.display = true;
触发了TypeScript编译错误:
Property 'title' does not exist on type '_DeepPartialObject<{ type: "linear"; } & CartesianScaleOptions & { beginAtZero: boolean; suggestedMin?: number; suggestedMax?: number; grace?: string | number; ticks: { format: NumberFormatOptions; precision: number; stepSize: number; count: number; }; }> | ... 4 more ... | _DeepPartialObject<...>'.
已知通过类型断言可以解决该问题,可行代码如下:
(this.chart.config.options.scales.y as CartesianScaleOptions).title.display = true;
但不想每次操作都进行类型断言,求更优解决方案。
内容的提问来源于stack exchange,提问作者devzero
相关产品推荐
相关产品推荐

