]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Require 'this' when calling tick formatters (#12064)
authorJosh Kelley <joshkel@gmail.com>
Fri, 18 Apr 2025 11:16:34 +0000 (07:16 -0400)
committerGitHub <noreply@github.com>
Fri, 18 Apr 2025 11:16:34 +0000 (13:16 +0200)
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')
```

src/types/index.d.ts
test/types/ticks/ticks.ts [new file with mode: 0644]

index 898a2c821cb7143c21ba7da7ec9c9feaa48d47b8..69a4cfccbc51fdebd485bd24e270c49556525525 100644 (file)
@@ -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 (file)
index 0000000..a5a9e28
--- /dev/null
@@ -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<CoreScaleOptions>'.
+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 }]);