Skip to content

Commit 3361a63

Browse files
authored
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') ```
1 parent f46572e commit 3361a63

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,15 +1519,15 @@ export declare const Ticks: {
15191519
* @param ticks the list of ticks being converted
15201520
* @return string representation of the tickValue parameter
15211521
*/
1522-
numeric(tickValue: number, index: number, ticks: { value: number }[]): string;
1522+
numeric(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string;
15231523
/**
15241524
* Formatter for logarithmic ticks
15251525
* @param tickValue the value to be formatted
15261526
* @param index the position of the tickValue parameter in the ticks array
15271527
* @param ticks the list of ticks being converted
15281528
* @return string representation of the tickValue parameter
15291529
*/
1530-
logarithmic(tickValue: number, index: number, ticks: { value: number }[]): string;
1530+
logarithmic(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string;
15311531
};
15321532
};
15331533

test/types/ticks/ticks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Chart, Ticks } from '../../../src/types.js';
2+
3+
// @ts-expect-error The 'this' context... is not assignable to method's 'this' of type 'Scale<CoreScaleOptions>'.
4+
Ticks.formatters.numeric(0, 0, [{ value: 0 }]);
5+
6+
const chart = new Chart('test', {
7+
type: 'line',
8+
data: {
9+
datasets: [{
10+
data: [{ x: 1, y: 1 }]
11+
}]
12+
},
13+
});
14+
15+
Ticks.formatters.numeric.call(chart.scales.x, 0, 0, [{ value: 0 }]);

0 commit comments

Comments
 (0)