0

I have the following options set:

export const options = {
  responsive: true,
  scales: {
    x: {
      ticks: {
        maxTicksLimit: 10,
        autoSkip: true,
        color: 'white',
      },
      type: 'time',
      adapters: {
        date: {
          locale: enGB,
        },
      },
    },
    y: {
      ticks: {
        color: 'white',
      },
    },
  },
  plugins: {
    legend: {
      display: false,
    },
    title: {
      display: true,
      text: 'IndexCoin Price',
      color: 'white',
    },
  },
}

which I then use in

 <Line options={options} data={dataSets} />

In this line vscode red underlines the first options with the problem:

"min" | "max"> & { min: string | number; max: string | number; suggestedMin: string | number; ... 4 more ...; ticks: { ...; }; }> | ... 4 more ... | undefined'.
          Type '{ ticks: { maxTicksLimit: number; autoSkip: boolean; color: string; }; type: string; adapters: { date: { locale: Locale; }; }; }' is not assignable to type '_DeepPartialObject<{ type: "timeseries"; } & Omit<CartesianScaleOptions, "min" | "max"> & { min: string | number; max: string | number; suggestedMin: string | number; ... 4 more ...; ticks: { ...; }; }>'.
            Types of property 'type' are incompatible.
              Type 'string' is not assignable to type '"timeseries"'.ts(2322)
types.d.ts(19, 5): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & Omit<ChartProps<"line", number[], Date>, "type"> & { ref?: ForwardedRef<ChartJSOrUndefined<"line", number[], Date>> | undefined; }'

This goes away if I remove type: 'time' in options. The code works fine though. Why is this happening?

3
  • Does this answer your question? Typescript Type 'string' is not assignable to type Commented May 25, 2022 at 11:20
  • @TobiasS. thank you for linking this answer. It seems very related but I don't understand how to apply the solution to my particular problem. What should I do? Commented May 25, 2022 at 11:23
  • 1
    put as const behind the object at the variable declaration Commented May 25, 2022 at 11:24

1 Answer 1

4

Setting the type explicitly should get rid of the type error.

import {
  ChartOptions
} from "chart.js";

const options: ChartOptions<"line"> = { /* ... */ };

For more information check out FAQ in react-chartjs-2 docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Setting the ChartOptions type explicitly makes it much easier to fix type problems of the options object when you have linting enabled.

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.