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 with backgroundColor on options parameter for scriptable options #917

Open
thomas-haufe opened this issue Nov 30, 2023 · 4 comments
Labels
types Typescript type changes

Comments

@stockiNail stockiNail added the types Typescript type changes label Dec 1, 2023
@stockiNail
Copy link
Collaborator

@thomas-haufe Thank you for feedback. The options passed by context need to be cast to the options related to the annotation type where the scriptable option is invoked. By default a annotation for line is used but line annotation doesn't have the backgroundColor as options.

The following code could solve the issue:

import { Chart, ChartData, ChartOptions, Color } from 'chart.js';
import annotationPlugin, {BoxAnnotationOptions} from 'chartjs-plugin-annotation';

Chart.register(annotationPlugin)

const options: ChartOptions<'bubble'> = {
  plugins: {
    annotation: {
      annotations: {
        firstQuadrantBg: {
          type: 'box',
          backgroundColor: '#fff',
          borderColor: (_, options) => {
            const boxOptions = options as BoxAnnotationOptions;
            return boxOptions.backgroundColor as Color;
          },
        }
      }
    }
  }
}

@thomas-haufe
Copy link
Author

Thanks for the idea @stockiNail. Are you proposing it as a final solution or as a temporary workaround until this is fixed properly?

I think this should be the job of the library and not the job of the consumer.

And it's probably possible to let chartjs correctly infer the type with some advanced type gymnastics? :)

@stockiNail
Copy link
Collaborator

@thomas-haufe currently the plugin is designed to cast the options when engaged in a scriptable options.
That said, this is a workaround till the scriptable context will be extended maybe in this lib to add the type.

@thomas-haufe
Copy link
Author

Wouldn't it work to change the following line

export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnnotationOptions) => T);

to something like this?
export type Scriptable<T, TContext, TYPE> = T | ((ctx: TContext, options: AnnotationOptions[TYPE]) => T); (or something similar)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Typescript type changes
Projects
None yet
Development

No branches or pull requests

2 participants