Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cyantree opened this issue Feb 18, 2023 · 8 comments · Fixed by #12012
Closed
Labels
type: bug type: types Typescript type changes

Comments

@cyantree
Copy link

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

@LeeLenaleee
Copy link
Collaborator

There does not seem to be any ts errors

image

@cyantree
Copy link
Author

@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.

@LeeLenaleee LeeLenaleee added the type: types Typescript type changes label Feb 20, 2023
@pete-mcwilliams
Copy link

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.

@etimberg
Copy link
Member

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.

@candelrg
Copy link

candelrg commented Jan 21, 2025

Hello.
This issue is still present in latest version of chart.js. (4.4.7)
With VSCode, when I get inside the getRelativePosition method, I can see the following class (helpers.dom.d.ts):

import type Chart from '../core/core.controller.js';
import type { ChartEvent } from '../types.js';
[...]
export declare function getRelativePosition(event: Event | ChartEvent | TouchEvent | MouseEvent, chart: Chart): {
    x: number;
    y: number;
};
[...]

The problem comes from the imports.
instead of

import type Chart from '../core/core.controller.js';
import type { ChartEvent } from '../types.js';

you should use

import type { Chart, ChartEvent } from '../types.js';

to make things work correctly inside typescript.
Otherwise, we get an error like

Argument of type 'Chart<keyof ChartTypeRegistry, (number | [number, number] | Point | BubbleDataPoint | null)[], unknown>' is not assignable to parameter of type 'Chart'.
  Type 'Chart<keyof ChartTypeRegistry, (number | [number, number] | Point | BubbleDataPoint | null)[], unknown>' is missing the following properties from type 'Chart': _options, _aspectRatio, _layers, _metasets, and 40 more.ts(2345)

This change must be done inside chart.js project

@etimberg
Copy link
Member

@candelrg are you able to send a PR with that change?

@candelrg
Copy link

Hello and sorry for the late answer.
I'm not sure how to, because I did not clone the github project yet (I discovered the code with what node installed).
But I will give it a try.

@candelrg
Copy link

candelrg commented Jan 23, 2025

Hello again.
Sorry, it is not possible for me to do a PR.
I failed to even clone the project with ssh from the place I work.
So, in order to help, I downloaded the zip to identify the concerned class, and I found it:
src/helpers/helpers.dom.ts

joshkel added a commit to joshkel/Chart.js that referenced this issue Jan 29, 2025
helpers.dom.ts functions referenced the internal `Chart` JavaScript class rather than the published `Chart<TType, TData, TLabel>` TypeScript definition. This causes errors when outside code tries to call helper functions.

The two `Chart` interfaces are incompatible - the `width`, `height`, and `currentDevicePixelRatio` properties are declared as readonly in the TS declaration but are manipulated by helpers.dom.ts functions, and helpers.dom.ts functions need to be invoked both by internal Chart.js code (which uses the JS class) and by outside code (which uses the TS types). To address this, I'm importing the JS version as `PrivateChart`. There may be a better solution.

It's my understanding that the comment about "typedefs are auto-exported" is obsolete now that helpers.dom is a native TS file.

Fixes chartjs#11153
joshkel added a commit to joshkel/Chart.js that referenced this issue Jan 29, 2025
helpers.dom.ts functions referenced the internal `Chart` JavaScript class rather than the published `Chart<TType, TData, TLabel>` TypeScript definition. This causes errors when outside code tries to call helper functions.

The two `Chart` interfaces are incompatible - the `width`, `height`, and `currentDevicePixelRatio` properties are declared as readonly in the TS declaration but are manipulated by helpers.dom.ts functions, and helpers.dom.ts functions need to be invoked both by internal Chart.js code (which uses the JS class) and by outside code (which uses the TS types). To address this, I'm importing the JS version as `PrivateChart`. There may be a better solution.

It's my understanding that the comment about "typedefs are auto-exported" is obsolete now that helpers.dom is a native TS file.

Fixes chartjs#11153
LeeLenaleee pushed a commit that referenced this issue Jan 30, 2025
helpers.dom.ts functions referenced the internal `Chart` JavaScript class rather than the published `Chart<TType, TData, TLabel>` TypeScript definition. This causes errors when outside code tries to call helper functions.

The two `Chart` interfaces are incompatible - the `width`, `height`, and `currentDevicePixelRatio` properties are declared as readonly in the TS declaration but are manipulated by helpers.dom.ts functions, and helpers.dom.ts functions need to be invoked both by internal Chart.js code (which uses the JS class) and by outside code (which uses the TS types). To address this, I'm importing the JS version as `PrivateChart`. There may be a better solution.

It's my understanding that the comment about "typedefs are auto-exported" is obsolete now that helpers.dom is a native TS file.

Fixes #11153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug type: types Typescript type changes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants