Skip to content
Source
Sign inSign up
TanStack/react-charts

[TypeScript] Misleading error messages in Chart options property #259

Send to my agent
ClosedZvezdin(GitHub profile, opens in a new tab) opened this issue 1 comment

Version: 3.0.0 beta 33

When using a chart in its basic form (Chart options={{data, primaryAxis, secondaryAxes}}/>), it is easy to forget that what you are passing isn't just a list of three variables (data and axis), but that their naming is important too. So a user might easily do something like: Chart options={{chartData, primaryAxis, secondaryAxes}}/>

You would expect this to produce an easy to debug error in the likes of "data, a required key in options, is not provided". However, the error that is produced is highlighted on the primaryAxis and secondaryAxes parameters, and speaks the following (ValuationPoint is some legit TDatum):

Type 'AxisOptions<ValuationPoint>' is not assignable to type 'AxisOptions<unknown>'.
  Type 'AxisTimeOptions<ValuationPoint>' is not assignable to type 'AxisOptions<unknown>'.
    Type 'AxisTimeOptions<ValuationPoint>' is not assignable to type 'AxisTimeOptions<unknown>'.
      Type 'AxisTimeOptions<ValuationPoint>' is not assignable to type '{ scaleType?: "time" | "localTime" | undefined; getValue: (datum: unknown) => ChartValue<Date>; min?: Date | undefined; max?: Date | undefined; hardMin?: Date | undefined; hardMax?: Date | undefined; formatters?: { ...; } | undefined; }'.
        Types of property 'getValue' are incompatible.
          Type '(datum: ValuationPoint) => ChartValue<Date>' is not assignable to type '(datum: unknown) => ChartValue<Date>'.
            Types of parameters 'datum' and 'datum' are incompatible.
              Type 'unknown' is not assignable to type 'ValuationPoint'.ts(2322)
types.d.ts(8, 5): The expected type comes from property 'primaryAxis' which is declared here on type 'ChartOptions<unknown>'

This is much harder to debug (it took me quite the time to get around it). Chart options={{data: chartData, primaryAxis, secondaryAxes}}/> gets the job done.

The correct error is shown if we omit the first variable in options:

Chart options={{primaryAxis, secondaryAxes}}/>

Resulting in an error on the 'options' prop: Property 'data' is missing in type '{ ....

  1. Zvezdin(GitHub profile, opens in a new tab) changed the title

  2. I’ll leave this error up to Typescript since it can and should be caught in development by a compiler.

  3. tannerlinsley closed this

Sign in to join this conversation.