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')
```
This commit is contained in:
Josh Kelley 2025-04-18 07:16:34 -04:00 committed by GitHub
parent f46572e19a
commit 3361a63705
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View 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;
};
};

15
test/types/ticks/ticks.ts Normal file
View File

@ -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 }]);