chartjs / chartjs/Chart.js

TimeScaleTimeOptions should be an interface so adapters can augment displayFormats

Open
#12,220 0 comments 0 reactions 0 assignees View on GitHub
type: enhancement
Dominant language
JavaScript
Stars
67.7k
Forks
11.9k
Avg merge
7h 39m
Merged PRs (30d)
5

Description

### Feature Proposal

`TimeScaleTimeOptions` is declared as a `type` alias, which prevents adapters from widening `displayFormats` or `tooltipFormat` via TypeScript module augmentation.

At runtime, Chart.js passes `displayFormats` values straight through to `adapter.format()` without any string-specific operations:

```ts
// chart.js/dist/chart.js line 11232
return this._adapter.format(value, fmt);
```

This means adapters can accept richer format types (e.g. Intl.DateTimeFormatOptions objects, callbacks) and everything works at runtime. But TypeScript rejects it because displayFormats is typed as { [key: string]: string } inside a type alias that can't be augmented.

### Context

- https://github.com/schummar/chartjs-adapter-temporal accepts Intl.DateTimeFormatOptions objects and callbacks in format() (https://github.com/schummar/chartjs-adapter-temporal/pull/7)
- Related: #12140 (proposal for an official Temporal/Intl adapter)
- The same limitation applies to tooltipFormat and to any other adapter that wants to accept non-string format values

### Possible Implementation

Change TimeScaleTimeOptions from a type to an interface:

```diff
- export type TimeScaleTimeOptions = {
+ export interface TimeScaleTimeOptions {
parser: string | ((v: unknown) => number);
round: false | TimeUnit;
isoWeekday: boolean | number;
displayFormats: {
[key: string]: string;
};
tooltipFormat: string;
unit: false | TimeUnit;
minUnit: TimeUnit;
}
```

This should be a non-breaking change — interface and type are interchangeable for object shapes. But it would allow adapters to augment displayFormats and tooltipFormat via declaration merging:

```
// In an adapter package
declare module 'chart.js' {
interface TimeScaleTimeOptions {
displayFormats: {
[key: string]: string | Intl.DateTimeFormatOptions | ((timestamp: number, context: FormatContext) => string);
};
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.