From: Josh Kelley Date: Fri, 18 Apr 2025 11:16:34 +0000 (-0400) Subject: Require 'this' when calling tick formatters (#12064) X-Git-Tag: v4.5.0~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3361a637052c2e51d5bcf077e727e06a53941a3a;p=thirdparty%2FChart.js.git Require 'this' when calling tick formatters (#12064) The `numeric` and `logarithmic` tick formatters require that `this` be provided. That happens automatically if they're used directly as a tick callback but not if they're invoked manually. Failing to pass `this` results in runtime errors similar to the following: ``` TypeError: Cannot read properties of undefined (reading 'chart') ``` --- diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 898a2c821..69a4cfccb 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1519,7 +1519,7 @@ export declare const Ticks: { * @param ticks the list of ticks being converted * @return string representation of the tickValue parameter */ - numeric(tickValue: number, index: number, ticks: { value: number }[]): string; + numeric(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string; /** * Formatter for logarithmic ticks * @param tickValue the value to be formatted @@ -1527,7 +1527,7 @@ export declare const Ticks: { * @param ticks the list of ticks being converted * @return string representation of the tickValue parameter */ - logarithmic(tickValue: number, index: number, ticks: { value: number }[]): string; + logarithmic(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string; }; }; diff --git a/test/types/ticks/ticks.ts b/test/types/ticks/ticks.ts new file mode 100644 index 000000000..a5a9e28be --- /dev/null +++ b/test/types/ticks/ticks.ts @@ -0,0 +1,15 @@ +import { Chart, Ticks } from '../../../src/types.js'; + +// @ts-expect-error The 'this' context... is not assignable to method's 'this' of type 'Scale'. +Ticks.formatters.numeric(0, 0, [{ value: 0 }]); + +const chart = new Chart('test', { + type: 'line', + data: { + datasets: [{ + data: [{ x: 1, y: 1 }] + }] + }, +}); + +Ticks.formatters.numeric.call(chart.scales.x, 0, 0, [{ value: 0 }]);