-
Notifications
You must be signed in to change notification settings - Fork 12k
Description
Expected behavior
After creating a Chart, I want to update its colors dynamically. I understand that I can't directly say chart.options.scales.x.title.color because the properties starting with scales are optional. However, I expect to be able to test chart.options.scales?.x?.title?.color === undefined, and if that's false, TypeScript should allow me to assign a new color to chart.options.scales.x.title.color.
Current behavior
Under certain conditions, TypeScript 4.7.4 emits an error when I try to access title:
Property 'title' does not exist on type '_DeepPartialObject<{ type: "linear"; } & CartesianScaleOptions & { beginAtZero: boolean; suggestedMin?: number | undefined; suggestedMax?: number | undefined; grace?: string | number | undefined; ticks: { ...; }; }> | ... 4 more ... | _DeepPartialObject<...>'.
Property 'title' does not exist on type '_DeepPartialObject<{ type: "radialLinear"; } & CoreScaleOptions & { animate: boolean; startAngle: number; angleLines: { display: boolean; color: Scriptable<Color, ScriptableScaleContext>; lineWidth: Scriptable<...>; borderDash: Scriptable<...>; borderDashOffset: Scriptable<...>; }; ... 7 more ...; ticks: RadialTickO...'.
The conditions appear to be:
- A
legendonClickhandler is specified. - The error goes away if I change the handler's third parameter from
LegendElement<ChartType>toany. - The error goes away if I omit the handler entirely.
- The error never happens with
ticks, only withtitle. I am not sure whytitleis special.
Reproducible sample
Optional extra steps/info to reproduce
Here's a CodePen (with TypeScript types commented out), showing that this works at runtime: https://codepen.io/StephanTLavavej/pen/GRxKJgz
TypeScript examples:
- original repro with error
- error vanishes if handler's third parameter is
any - error vanishes if handler is removed entirely
Possible solution
No response
Context
I'm implementing dark mode for the microsoft/STL Status Chart at https://microsoft.github.io/STL/ . I converted it to use TypeScript a couple of months ago, but haven't tried to change colors dynamically until now.
chart.js version
v3.8.0
Browser name and version
Chrome 103.0.5060.53
Link to your project
No response