Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Sengulair opened this issue Apr 12, 2023 · 0 comments · Fixed by #11569
Closed
Labels
type: bug type: types Typescript type changes

Comments

@Sengulair
Copy link
Contributor

Sengulair commented Apr 12, 2023

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

@Sengulair Sengulair changed the title [TypeScript] Chart argument for hooks lack typed generic with TType in Plugin interface [TypeScript] Chart argument for hooks lack typed generic with TType in Plugin interface Apr 12, 2023
@LeeLenaleee LeeLenaleee added the type: types Typescript type changes label Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug type: types Typescript type changes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants