Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

TypeScript: `chartHelpers.getRelativePosition(new Event(''), new Chart())` doesn't pass

Open cyantree opened this issue 2 years ago • 4 comments

Expected behavior

When passing a Chart instance to the helper method getRelativePosition() I expect the types to be correct. In v3 this worked.

Current behavior

The types don't match and the following error will be shown:

const chart: Chart<"bar", number[], string>
Argument of type 'Chart<"bar", number[], string>' is not assignable to parameter of type 'Chart'.
  Type 'Chart<"bar", number[], string>' is missing the following properties from type 'Chart': _options, _aspectRatio, _layers, _metasets, and 41 more.ts(2345)

Reproducible sample

https://codesandbox.io/s/typescript-playground-export-forked-ufm45w?file=/index.ts:435-491

Optional extra steps/info to reproduce

Open the sandbox, ignore the runtime error and just look at the TS compiler error.

Possible solution

No response

Context

No response

chart.js version

v4.2.1

Browser name and version

No response

Link to your project

No response

cyantree avatar Feb 18 '23 22:02 cyantree

There does not seem to be any ts errors

image

LeeLenaleee avatar Feb 19 '23 14:02 LeeLenaleee

@LeeLenaleee It seems you've run the example on a mobile device, correct? Please try again on a desktop computer as the mobile version doesn't seem to run type checking.

cyantree avatar Feb 19 '23 20:02 cyantree

Just ran into same issue in latest after upgrade from v3...

Error: libs/charts/src/lib/components/line/line.component.ts:74:69 - error TS2345: Argument of type 'Chart<"line", (number | Point | null)[], unknown>' is not assignable to parameter of type 'Chart'. Type 'Chart<"line", (number | Point | null)[], unknown>' is missing the following properties from type 'Chart': _options, _aspectRatio, _layers, _metasets, and 40 more.

Had to cast my chart to any.

pete-mcwilliams avatar Oct 09 '23 16:10 pete-mcwilliams

I'm guessing the unknown on the chart type is the problem which comes from https://github.com/chartjs/Chart.js/blob/master/src/types/index.d.ts#L487

When the chart has data, the unknown is then inferred from the data type.

etimberg avatar Oct 10 '23 14:10 etimberg