-
Notifications
You must be signed in to change notification settings - Fork 647
Description
When using Tooltip with labelFormatter in a TypeScript project, the expected function signature is too restrictive and causes a TypeScript error when the formatter is typed more strictly (e.g., (t: number) => string).
The current typing requires:
(label: ReactNode, payload: readonly Payload<ValueType, NameType>[]) => ReactNode
This makes it impossible to strongly type label as number, even when the corresponding XAxis is explicitly defined as numeric.
TS2322: Type '(t: number) => string' is not assignable to type
'((label: ReactNode, payload: readonly Payload<ValueType, NameType>[]) => ReactNode)
& ((label: any, payload: Payload<ValueType, NameType>[]) => ReactNode)'.
Type '(t: number) => string' is not assignable to type
'(label: ReactNode, payload: readonly Payload<ValueType, NameType>[]) => ReactNode'.
Types of parameters 't' and 'label' are incompatible.
Type 'ReactNode' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.import { LineChart, Line, XAxis, YAxis, Tooltip } from 'recharts';
<LineChart data={data}>
<XAxis dataKey="timestamp" type="number" />
<YAxis />
<Tooltip
labelFormatter={(t: number) =>
new Date(t).toLocaleTimeString()
}
/>
<Line dataKey="avg" />
</LineChart>Expected Behavior
If XAxis is defined as:
<XAxis dataKey="timestamp" type="number" />
Then labelFormatter should allow:
(label: number) => ReactNode
Or the type should be widened to something more practical such as:
(label: unknown, payload: Payload[]) => ReactNode
This would allow proper narrowing inside the formatter without forcing unsafe casts.
Workaround
Currently, the only safe workaround is:
<Tooltip
labelFormatter={(label) =>
new Date(Number(label)).toLocaleTimeString()
}
/>
But this removes type safety and forces casting.