You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:52:41