Skip to content

TimeScaleTimeOptions should be an interface so adapters can augment displayFormats #12220

@johanrd

Description

@johanrd

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:

  // 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

Possible Implementation

Change TimeScaleTimeOptions from a type to an interface:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions