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

Type error when configuring colors plugin. #11114

Closed
jaybeeuu opened this issue Feb 3, 2023 · 2 comments · Fixed by #11115
Closed

Type error when configuring colors plugin. #11114

jaybeeuu opened this issue Feb 3, 2023 · 2 comments · Fixed by #11115
Labels
type: bug type: types Typescript type changes

Comments

@jaybeeuu
Copy link

jaybeeuu commented Feb 3, 2023

Expected behavior

Following the examples in the docs, and when using a typescript project, the colors plugin can be configured, without type errors.

Current behavior

The colors plugin is not included in PluginOptionsByType. In typescript project trying to follow the examples in the docs leads to a type error.

image

Reproducible sample

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgYQBYENYBoUQDbQDOcAvnAGZQQhwDkAxhrAHQBWhtA3AFDdqYxmUAKYBzYIRjCoACmT4iASh7d6EAHaS4wvMJoBeOABMI9AK4hh6wfRHopAUV2XrMhunUA3dB2W91wgDuKEwwMjp6OAjccLHG9ugAXIjxMD7CMISJANoAuqRYMXEwAJ5gwsm0eMABtIVxcBBgMMAaWYhFDbFgeGbimslIagRQ7UhW6ABGukbJMFBmwqSknSskfkA

Optional extra steps/info to reproduce

No response

Possible solution

One fix is really easy (and i have a branch ready to go if you want it...) which is to add the type into PluginOptionsByType

// /src/types/index.d.ts:2795

 export interface PluginOptionsByType<TType extends ChartType> {
+  colors: ColorsPluginOptions;
   decimation: DecimationOptions;
   filler: FillerOptions;
   legend: LegendOptions<TType>;
   subtitle: TitleOptions;
   title: TitleOptions;
   tooltip: TooltipOptions<TType>;
}

I wonder if it's been omitted on purpose though? Looking at the other plugins in there they are not ones you need to call register for (i.e. they appear to be "preregistered plugins"). That's quite reasonable since adding it into PluginOptionsByType makes it appear from the types to be available all the time, but you have to register it before you can use it. But in that case, it feels like there is an expectation that some declaration merging has to happen (as with the plugins that live in other packages) - doing that as a user feels a bit awkward.

So I wonder if the solution might be to expose the Colors plugin on an import path rather than on the root e.g. import { Colors } from 'chart.js/colors'; you would be able to add the declaration merge in a .d.ts on that path and give the users a better experience, and retain a similar behaviour to other plugins.

Context

No response

chart.js version

v4.2.0

Browser name and version

No response

Link to your project

No response

@LeeLenaleee LeeLenaleee added the type: types Typescript type changes label Feb 3, 2023
@LeeLenaleee
Copy link
Collaborator

That would be a good fix

@jaybeeuu
Copy link
Author

jaybeeuu commented Feb 9, 2023

Thanks!

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