Skip to content

[TypeScript] Chart argument for hooks lack typed generic with TType in Plugin interface  #11228

Closed
@Sengulair

Description

@Sengulair

Expected behavior

export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> extends ExtendedPlugin<TType, O> {
  afterInit?(chart: Chart<TType>, args: EmptyObject, options: O): void;
  // ...and so on
}

I'm wondering why the generic argument TType is not provided to all of the hooks for the Chart interface. It will narrow the type and help write more specific plugins for different types of charts.

Current behavior

Plugin interface

export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> extends ExtendedPlugin<TType, O> {
  afterInit?(chart: Chart, args: EmptyObject, options: O): void;
}

Reproducible sample

Playground link

Possible solution

I'm not sure why the generic argument TType it's not passed down to the Chart class, because the Plugin interface already takes the TType generic argument, so it would be easy to add it to all of the hooks chart argument.

export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> extends ExtendedPlugin<TType, O> {
  beforeInit?(chart: Chart<TType>, args: EmptyObject, options: O): void;
  afterInit?(chart: Chart<TType>, args: EmptyObject, options: O): void;
  // ...and so on
}

Context

I've modified the options for the dataset for bar charts with TypeScript's declaration merging and I want to use this new highlightedColor property inside of the plugin without type errors. But plugin hooks have only a generic Chart interface without argument, so I can't use any custom dataset option inside to be typesafe.

declare module 'chart.js' {
  interface BarControllerDatasetOptions {
    highlightedColor: string;
  }
}

chart.js version

v4.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions