Skip to content

TS: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' #9487

Answered by etimberg
DavideViolante asked this question in Q&A
Discussion options

You must be logged in to vote

I looked into this and ChartItem is defined as:

export declare type ChartItem =
  | string
  | CanvasRenderingContext2D
  | OffscreenCanvasRenderingContext2D
  | HTMLCanvasElement
  | OffscreenCanvas
  | { canvas: HTMLCanvasElement | OffscreenCanvas }
  | ArrayLike<CanvasRenderingContext2D | HTMLCanvasElement | OffscreenCanvas>;

Passing any HTMLElement is not supported by the constructor, so you will need to cast the return of getElementById to an HTMLCanvasElement

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…

Replies: 10 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by kurkle
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
type: support type: types Typescript type changes
2 participants
Converted from issue

This discussion was converted from issue #9427 on July 28, 2021 11:00.