0

I am trying to define a callback function in my chartJS options. for some reason, i get this error on my label definition:

Type '(tooltipItem: ChartTooltipItem, data: ChartData) => number | number[] | ChartPoint' is not assignable to type '(tooltipItem: ChartTooltipItem, data: ChartData) => string | string[]'. What am I doing wrong?

options: {
    tooltips: {
        callbacks: {
          title: (tooltipItems, data) => {
            return '';
          },
            label: (tooltipItem, data) => {
            const datasetLabel = '';
            return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
          }
        }
11
  • 1
    Hi, Well it's basically what the error is telling you, your returning string & string[ ] when number types expected, if you provide a stackblitz or add details on your example that repeats your error I gladley fork or give example code. Else i recommend reading this and this SO posts. Commented Nov 28, 2020 at 21:02
  • It may be too large for a stackblitz but its compiling fine but it justs has that red underline error. it looks like it's coming from the library node_modules/@types/chart.js/index.d.ts:242:9 242 label?(tooltipItem: ChartTooltipItem, data: ChartData): string | string[]; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The expected type comes from property 'label' which is declared here on type 'ChartTooltipCallback' Commented Nov 30, 2020 at 16:41
  • Not sure what you mean to big, you only need to replicate the issue itself. It's from the typings folder for the library Commented Nov 30, 2020 at 20:31
  • Its weird but I got it working by just adding the any type to the parameters like this: label: (tooltipItem, data: any) Commented Nov 30, 2020 at 21:21
  • 1
    the correct way would be so figure out your typing issues if it's the typings/lib thats the problem itself if so using "any" is one way to go :) Commented Nov 30, 2020 at 21:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.