Skip to content

Unable to disable global plugins in typescript #11398

@stockiNail

Description

@stockiNail

Expected behavior

As reported from the doc, how to disable plugins https://www.chartjs.org/docs/latest/developers/plugins.html#disable-plugins, you can disable the invocation of a plugin registered globally setting in the chart options as false.

Chart.register({
    id: 'p1',
    // ...
});

const chart = new Chart(ctx, {
    options: {
        plugins: {
            p1: false   // disable plugin 'p1' for this instance
        }
    }
});

The same when you want to disable all plugins, as reported to the doc:

const chart = new Chart(ctx, {
    options: {
        plugins: false // all plugins are disabled for this instance
    }
});

Current behavior

When you use TS, you can not disable the plugins as documented, getting the error:

index.ts:28:4 - error TS2559: Type 'false' has no properties in common with type '_DeepPartialObject<LegendOptions<"bar">>'.

28    legend: false,
      ~~~~~~

  node_modules/chart.js/dist/types/index.d.ts:2909:3
    2909   legend: LegendOptions<TType>;
           ~~~~~~
    The expected type comes from property 'legend' which is declared here on type '_DeepPartialObject<PluginOptionsByType<"bar">>'

Reproducible sample

https://github.com/stockiNail/chartjs-11288

Optional extra steps/info to reproduce

  1. edit index.ts
  2. set
options: {
   plugins: {
     legend: false,
   }
 }
  1. execute npm run test

Possible solution

Add to PluginOptionsByType interface items the options to be set to false.
Example:

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

Add to PluginChartOptions interface plugins items the options to be set to false.

export interface PluginChartOptions<TType extends ChartType> {
  plugins: PluginOptionsByType<TType> | false;
}

Context

No response

chart.js version

v4.3.0

Browser name and version

FF 115.0.1

Link to your project

https://github.com/stockiNail/chartjs-11288

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