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 Compile Error for Mixed Chart Type #358

Open
isaacsando opened this issue Dec 23, 2022 · 5 comments
Open

TypeScript Compile Error for Mixed Chart Type #358

isaacsando opened this issue Dec 23, 2022 · 5 comments

Comments

@isaacsando
Copy link

Issue

I'm unable to get the below TypeScript to compile due to a type error when using a mixed chart type. According to the Chart.js documentation the type property is set on each dataset when creating a mixed chart (not outside of it like all other charts). The chartjs-plugin-datalabels documentation states that the plugin options can be set per data level so I'm unsure as to why this error is happening. Any insight is appreciated. Thank you!

import { Chart } from "chart.js/auto";
import ChartDataLabels from "chartjs-plugin-datalabels";

const chart = new Chart(<HTMLCanvasElement>document.getElementById("mixed"), {
  plugins: [ChartDataLabels],
  data: {
    datasets: [
      {
        // Change options only for labels of THIS DATASET
        datalabels: {         // <-- these three lines cause the error
          color: "#FFCE56",   //
        },                    //
        type: "bubble",
        label: "Bar Dataset",
        data: [
          // 10, 20, 30, 40,
          { x: 15, y: 20, r: 10 },
          { x: 25, y: 10, r: 20 },
        ],
      },
      {
        type: "line",
        label: "Line Dataset",
        data: [
          { x: 10, y: 10 },
          { x: 20, y: 20 },
          { x: 30, y: 30 },
        ],
      },
    ],
  },
});

Here is the full error:

[tsl] ERROR in /home/isaac/typescript/my-project/src/index.ts(10,9)
      TS2345: Argument of type '{ plugins: Plugin<keyof ChartTypeRegistry, AnyObject>[]; data: { datasets: ({ datalabels: { color: string; }; type: "bubble"; label: string; data: { x: number; y: number; r: number; }[]; } | { ...; })[]; }; }' is not assignable to parameter of type 'ChartConfiguration<keyof ChartTypeRegistry, { x: number; y: number; r: number; }[] | { x: number; y: number; }[], unknown> | ChartConfigurationCustomTypesPerDataset<keyof ChartTypeRegistry, { ...; }[] | { ...; }[], unknown>'.
  Type '{ plugins: Plugin<keyof ChartTypeRegistry, AnyObject>[]; data: { datasets: ({ datalabels: { color: string; }; type: "bubble"; label: string; data: { x: number; y: number; r: number; }[]; } | { ...; })[]; }; }' is not assignable to type 'ChartConfigurationCustomTypesPerDataset<keyof ChartTypeRegistry, { x: number; y: number; r: number; }[] | { x: number; y: number; }[], unknown>'.
    The types of 'data.datasets' are incompatible between these types.
      Type '({ datalabels: { color: string; }; type: "bubble"; label: string; data: { x: number; y: number; r: number; }[]; } | { type: "line"; label: string; data: { x: number; y: number; }[]; })[]' is not assignable to type 'ChartDatasetCustomTypesPerDataset<keyof ChartTypeRegistry, { x: number; y: number; r: number; }[] | { x: number; y: number; }[]>[]'.
        Type '{ datalabels: { color: string; }; type: "bubble"; label: string; data: { x: number; y: number; r: number; }[]; } | { type: "line"; label: string; data: { x: number; y: number; }[]; }' is not assignable to type 'ChartDatasetCustomTypesPerDataset<keyof ChartTypeRegistry, { x: number; y: number; r: number; }[] | { x: number; y: number; }[]>'.
          Type '{ datalabels: { color: string; }; type: "bubble"; label: string; data: { x: number; y: number; r: number; }[]; }' is not assignable to type 'ChartDatasetCustomTypesPerDataset<keyof ChartTypeRegistry, { x: number; y: number; r: number; }[] | { x: number; y: number; }[]>'.
            Object literal may only specify known properties, and 'datalabels' does not exist in type '_DeepPartialObject<{ type: "bubble"; } & BubbleControllerDatasetOptions> & ChartDatasetPropertiesCustomTypesPerDataset<keyof ChartTypeRegistry, { ...; }[] | { ...; }[]>'.

Environment Info

I'm using VS Code for editing and Webpack when compiling.

Chart.js: 4.0.1
chartjs-plugin-datalabels: 2.2.0

TypeScript: 4.9.4
tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "target": "es5",
    "module": "es6",
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "sourceMap": true
  }
}

webpack.config.js

const path = require("path");

module.exports = {
  entry: "./src/index.ts",
  devtool: "inline-source-map",
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
};
@hallofblood
Copy link

I have the exact same problem using Ionic and Angular

@isaacsando
Copy link
Author

I was able to get this to compile by changing the interface name ChartDatasetProperties to ChartDatasetPropertiesCustomTypesPerDataset in types/index.d.ts:

  interface ChartDatasetProperties<TType extends ChartType, TData> {

to

  interface ChartDatasetPropertiesCustomTypesPerDataset<TType extends ChartType, TData> {

It seems to work as expected after making this change.

@theDanielJLewis
Copy link

theDanielJLewis commented Feb 26, 2023

Any updates on fixing this in core?

I get this problem when I simply declare the options in options.plugins.datalabels.

@cyrilf
Copy link

cyrilf commented Jun 13, 2024

This issue is still active.. I keep having my TS linter complaining.

@kasiabienkowska
Copy link

Any news on this issue? I am also having the same problem, I have chart js v 3.9.1 and chartjs-plugin-datalabels v 2.2.0 and my ts linter is complaining for data labels with error TS2353

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

No branches or pull requests

5 participants