TS: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' #9487
-
Expected BehaviorNo error? Current Behavior
Possible Solutionidk Steps to ReproduceI just upgraded from 3.3.2 to 3.4.x ContextAngular project: import Chart from 'chart.js/auto';
function generateBarChart(res: any): any {
const chartElem = document.getElementById(`bar-chart`);
const data = {
labels: res.labels
datasets: [{ label: `Number of items`, data: res.data }],
};
const chartConfig = {
type: 'bar',
data,
options: {
...
},
};
return new Chart(chartElem, chartConfig); // <--- line of error
} Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
I looked into this and export declare type ChartItem =
| string
| CanvasRenderingContext2D
| OffscreenCanvasRenderingContext2D
| HTMLCanvasElement
| OffscreenCanvas
| { canvas: HTMLCanvasElement | OffscreenCanvas }
| ArrayLike<CanvasRenderingContext2D | HTMLCanvasElement | OffscreenCanvas>; Passing any import Chart from 'chart.js/auto';
function generateBarChart(res: any): any {
const chartElem = <HTMLCanvasElement>document.getElementById(`bar-chart`);
const data = {
labels: res.labels
datasets: [{ label: `Number of items`, data: res.data }],
};
const chartConfig = {
type: 'bar',
data,
options: {
...
},
};
return new Chart(chartElem, chartConfig);
} |
Beta Was this translation helpful? Give feedback.
-
Ok thanks, I also had to cast the type of chartConfig as ChartConfiguration to prevent this type error:
So the final code became: import { ChartConfiguration } from 'chart.js';
import Chart from 'chart.js/auto';
const chartElem = document.getElementById(`bar-chart-week${idSuffix}`) as HTMLCanvasElement;
...
const chartConfig = {
...
} as ChartConfiguration;
... I would recommend to add a documentation about this somewhere for TypeScript users (and Angular users). |
Beta Was this translation helpful? Give feedback.
-
It looks like TS is having trouble inferring the type from the options. You don't need a cast though, if you declare the const chartConfig: ChartConfiguration = {
type: 'bar',
data,
options: {},
}; |
Beta Was this translation helpful? Give feedback.
-
If I use const chartConfig: ChartConfiguration = { Then the line with
|
Beta Was this translation helpful? Give feedback.
-
If I edit index.esm.d.ts at line 3299 with this, the above error goes away: ...
export interface ChartTypeRegistry {
[bar: string]: { // line 3299, it was bar: {
chartOptions: BarControllerChartOptions;
... Inspired from: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html |
Beta Was this translation helpful? Give feedback.
-
What version of typescript are you using? |
Beta Was this translation helpful? Give feedback.
-
With 4.3.2 (VS Code version) I got the error, with 4.2.4 no (package.json version). |
Beta Was this translation helpful? Give feedback.
-
Ok, I tested with 4.1.x and it worked fine. There are other things broken starting in 4.2.0 (#9328 for example) |
Beta Was this translation helpful? Give feedback.
-
@DavideViolante are you able to test this again with chart.js v3.5.0? The types should work better for TS >= 4.2.0 |
Beta Was this translation helpful? Give feedback.
-
It's working with the same code as before, using |
Beta Was this translation helpful? Give feedback.
I looked into this and
ChartItem
is defined as:Passing any
HTMLElement
is not supported by the constructor, so you will need to cast the return ofgetElementById
to anHTMLCanvasElement