Skip to content

Commit 370f6c3

Browse files
authored
Fix helpers Chart type (#12012)
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
1 parent f744621 commit 370f6c3

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/helpers/helpers.dom.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import type {ChartArea, Scale} from '../types/index.js';
2-
import type Chart from '../core/core.controller.js';
3-
import type {ChartEvent} from '../types.js';
2+
import type PrivateChart from '../core/core.controller.js';
3+
import type {Chart, ChartEvent} from '../types.js';
44
import {INFINITY} from './helpers.math.js';
55

6-
/**
7-
* Note: typedefs are auto-exported, so use a made-up `dom` namespace where
8-
* necessary to avoid duplicates with `export * from './helpers`; see
9-
* https://github.com/microsoft/TypeScript/issues/46011
10-
* @typedef { import('../core/core.controller.js').default } dom.Chart
11-
* @typedef { import('../../types').ChartEvent } ChartEvent
12-
*/
13-
146
/**
157
* @private
168
*/
@@ -112,7 +104,7 @@ function getCanvasPosition(
112104

113105
export function getRelativePosition(
114106
event: Event | ChartEvent | TouchEvent | MouseEvent,
115-
chart: Chart
107+
chart: Chart | PrivateChart
116108
): { x: number; y: number } {
117109
if ('native' in event) {
118110
return event;
@@ -214,16 +206,16 @@ export function getMaximumSize(
214206
* @returns True if the canvas context size or transformation has changed.
215207
*/
216208
export function retinaScale(
217-
chart: Chart,
209+
chart: Chart | PrivateChart,
218210
forceRatio: number,
219211
forceStyle?: boolean
220212
): boolean | void {
221213
const pixelRatio = forceRatio || 1;
222214
const deviceHeight = Math.floor(chart.height * pixelRatio);
223215
const deviceWidth = Math.floor(chart.width * pixelRatio);
224216

225-
chart.height = Math.floor(chart.height);
226-
chart.width = Math.floor(chart.width);
217+
(chart as PrivateChart).height = Math.floor(chart.height);
218+
(chart as PrivateChart).width = Math.floor(chart.width);
227219

228220
const canvas = chart.canvas;
229221

@@ -238,7 +230,7 @@ export function retinaScale(
238230
if (chart.currentDevicePixelRatio !== pixelRatio
239231
|| canvas.height !== deviceHeight
240232
|| canvas.width !== deviceWidth) {
241-
chart.currentDevicePixelRatio = pixelRatio;
233+
(chart as PrivateChart).currentDevicePixelRatio = pixelRatio;
242234
canvas.height = deviceHeight;
243235
canvas.width = deviceWidth;
244236
chart.ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);

test/types/helpers/dom.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getRelativePosition } from '../../../src/helpers/helpers.dom.js';
2+
import { Chart, ChartOptions } from '../../../src/types.js';
3+
4+
const chart = new Chart('test', {
5+
type: 'line',
6+
data: {
7+
datasets: []
8+
}
9+
});
10+
11+
getRelativePosition(new MouseEvent('click'), chart);

0 commit comments

Comments
 (0)