Skip to content

fix(types): propagate Tooltip types in chart helper contexts#7125

Merged
PavelVanecek merged 8 commits into
recharts:mainfrom
mixelburg:fix/typed-tooltip-in-chart-helpers
May 24, 2026
Merged

fix(types): propagate Tooltip types in chart helper contexts#7125
PavelVanecek merged 8 commits into
recharts:mainfrom
mixelburg:fix/typed-tooltip-in-chart-helpers

Conversation

@mixelburg

@mixelburg mixelburg commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Problem

When Tooltip is included in the components map passed to createHorizontalChart(), createVerticalChart(), createCentricChart(), or createRadialChart(), the returned Typed.Tooltip component retains its original generic types (ValueType | NameType) instead of inheriting the chart's TNumerical/TData context.

This means Typed.Tooltip's formatter receives ValueType | undefined as the first argument rather than the expected TNumerical type, and the series name comes through as NameType (i.e. string | number) rather than a specific key of TData.

Fixes #7119

Solution

Added a 'Tooltip' branch to the component key mapping in:

  • TypedHorizontalChartContext (in createCartesianCharts.tsx)
  • TypedVerticalChartContext (in createCartesianCharts.tsx)
  • TypedCentricChartContext (in createPolarCharts.tsx)
  • TypedRadialChartContext (in createPolarCharts.tsx)

The mapped type is TooltipProps<Extract<TNumerical, ValueType>, Extract<keyof TData, NameType>>, which:

  • Narrows the value to TNumerical (e.g. number for a chart parameterised with TNumerical = number)
  • Narrows the name to the string/number keys of TData (e.g. 'value' | 'name' for ExampleDataPoint)

Type-level test

Added a test to test/util/createChartHelpers.spec.tsx that:

  1. Verifies Typed.Tooltip references the original Tooltip component at runtime
  2. Confirms a formatter typed as (value: number, name: 'value' | 'name') compiles without errors
  3. Confirms a formatter typed with a wrong value type (string instead of number) produces a TypeScript error

All 15 tests pass (npx vitest run test/util/createChartHelpers.spec.tsx).

Summary by CodeRabbit

  • Refactor

    • Improved chart Tooltip typing across cartesian and polar chart contexts to provide more accurate editor assistance and catch tooltip configuration issues earlier, ensuring tooltip components receive correctly typed value and name parameters.
  • Tests

    • Added tests validating tooltip formatter type safety, confirming correct parameter types and surfacing type errors for invalid usages.

…rticalChart/createCentricChart/createRadialChart

When Tooltip is included in the components map passed to createHorizontalChart(), createVerticalChart(), createCentricChart(), or createRadialChart(), the returned component was typed as the original generic Tooltip (using ValueType/NameType defaults) instead of the chart's TNumerical and TData key types.

Added 'Tooltip' to the type mapping in TypedHorizontalChartContext, TypedVerticalChartContext, TypedCentricChartContext, and TypedRadialChartContext so that Typed.Tooltip.formatter receives the correct value type (TNumerical) and name type (Extract<keyof TData, NameType>).

Closes recharts#7119
@coderabbitai

coderabbitai Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Special-cases the 'Tooltip' key in typed chart-context mapped types to use explicit TooltipProps with extracted numeric and name types; adds imports for TooltipProps, NameType, and ValueType; and adds a test ensuring Tooltip formatter typing for horizontal charts.

Changes

Cohort / File(s) Summary
Cartesian Chart Types
src/util/createCartesianCharts.tsx
Special-case the Tooltip key in TypedHorizontalChartContext and TypedVerticalChartContext to map to React.ComponentType<TooltipProps<Extract<TNumerical, ValueType>, Extract<keyof TData, NameType>>>. Added imports for TooltipProps, NameType, and ValueType.
Polar Chart Types
src/util/createPolarCharts.tsx
Special-case the Tooltip key in TypedCentricChartContext and TypedRadialChartContext to map to React.ComponentType<TooltipProps<Extract<TNumerical, ValueType>, Extract<keyof TData, NameType>>>. Added imports for TooltipProps, NameType, and ValueType.
Type Safety Test
test/util/createChartHelpers.spec.tsx
Adds a test asserting the Tooltip formatter on a horizontally-typed chart accepts the correct numeric value and data-key name types and flags incorrect value types as TypeScript errors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • ckifer
  • PavelVanecek
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(types): propagate Tooltip types in chart helper contexts' is concise, specific, and accurately summarizes the main change of fixing TypeScript typing for Tooltip components in chart helper contexts.
Description check ✅ Passed The description is comprehensive and well-structured, covering the problem statement, solution, implementation details, and type-level testing. It references the linked issue #7119 and explains the specific TypeScript changes made.
Linked Issues check ✅ Passed The PR fully addresses issue #7119 by implementing proper type narrowing for Tooltip components in all four chart helper contexts, with formatter receiving the correct TNumerical type and precise keyof TData for series names, plus comprehensive type-level testing.
Out of Scope Changes check ✅ Passed All changes are directly in scope: type updates to createCartesianCharts.tsx and createPolarCharts.tsx (fixing Tooltip typing), plus test additions in createChartHelpers.spec.tsx (type-level validation). No extraneous changes detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/util/createChartHelpers.spec.tsx (1)

172-203: Consider adding type-level tests for vertical, centric, and radial charts.

While the type mapping implementation is identical across all chart contexts, adding similar type-level tests for createVerticalChart, createCentricChart, and createRadialChart would provide comprehensive coverage and guard against regressions if the implementations diverge in the future.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/util/createChartHelpers.spec.tsx` around lines 172 - 203, Add the same
type-level test case you have for createHorizontalChart but duplicated for
createVerticalChart, createCentricChart, and createRadialChart: for each
createXChart call (createVerticalChart, createCentricChart, createRadialChart)
instantiate the typed chart with the same generics <ExampleDataPoint, string,
number>, compose Area, XAxis, Tooltip (or their relevant axis/area equivalents),
then assert Typed.Tooltip exists and add a valid formatter test (formatter:
(value: number, name: 'value' | 'name') => ...) plus an invalid formatter
annotated with // `@ts-expect-error` (value: string) to ensure type errors are
caught; mirror the structure used in the existing createHorizontalChart test and
reuse ExampleDataPoint, data, AreaChart, Area, XAxis, Tooltip identifiers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/util/createChartHelpers.spec.tsx`:
- Around line 172-203: Add the same type-level test case you have for
createHorizontalChart but duplicated for createVerticalChart,
createCentricChart, and createRadialChart: for each createXChart call
(createVerticalChart, createCentricChart, createRadialChart) instantiate the
typed chart with the same generics <ExampleDataPoint, string, number>, compose
Area, XAxis, Tooltip (or their relevant axis/area equivalents), then assert
Typed.Tooltip exists and add a valid formatter test (formatter: (value: number,
name: 'value' | 'name') => ...) plus an invalid formatter annotated with //
`@ts-expect-error` (value: string) to ensure type errors are caught; mirror the
structure used in the existing createHorizontalChart test and reuse
ExampleDataPoint, data, AreaChart, Area, XAxis, Tooltip identifiers.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 90cf4629-1d92-4e06-9b11-2aa35913e0c9

📥 Commits

Reviewing files that changed from the base of the PR and between 0fa4cc3 and b4d7d31.

📒 Files selected for processing (3)
  • src/util/createCartesianCharts.tsx
  • src/util/createPolarCharts.tsx
  • test/util/createChartHelpers.spec.tsx

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/util/createChartHelpers.spec.tsx (1)

172-201: Expand Tooltip type assertions to the other updated chart helper contexts.

Great addition for createHorizontalChart. Since this PR also updates Tooltip typing in vertical/centric/radial contexts, add at least one matching formatter type assertion for each to guard those paths too.

As per coding guidelines, "Aim for 100% unit test code coverage when writing new code".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/util/createChartHelpers.spec.tsx` around lines 172 - 201, Add matching
Tooltip formatter type assertions for the other chart helper factories updated
in this PR: createVerticalChart, createCentricChart, and createRadialChart. For
each factory (e.g., createVerticalChart<ExampleDataPoint, string, number>()),
assert the factory.Tooltip equals the imported Tooltip and add a valid formatter
test that types formatter as (value: number, name: 'value'|'name') => string,
plus an invalid `@ts-expect-error` case where formatter uses the wrong value type
(string) to ensure type errors are caught. Keep the tests structured like the
existing createHorizontalChart block, using AreaChart/XAxis/Tooltip components
from that factory.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/util/createChartHelpers.spec.tsx`:
- Around line 172-201: Add matching Tooltip formatter type assertions for the
other chart helper factories updated in this PR: createVerticalChart,
createCentricChart, and createRadialChart. For each factory (e.g.,
createVerticalChart<ExampleDataPoint, string, number>()), assert the
factory.Tooltip equals the imported Tooltip and add a valid formatter test that
types formatter as (value: number, name: 'value'|'name') => string, plus an
invalid `@ts-expect-error` case where formatter uses the wrong value type (string)
to ensure type errors are caught. Keep the tests structured like the existing
createHorizontalChart block, using AreaChart/XAxis/Tooltip components from that
factory.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c847cd8b-4856-4b6d-83a1-cb385b8f6ddf

📥 Commits

Reviewing files that changed from the base of the PR and between fd9cd8f and da04142.

📒 Files selected for processing (1)
  • test/util/createChartHelpers.spec.tsx

@codecov

codecov Bot commented Mar 15, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 34.15kB (0.68%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.3MB 8.63kB (0.67%) ⬆️
recharts/bundle-es6 1.13MB 8.16kB (0.73%) ⬆️
recharts/bundle-umd 547.75kB 1.95kB (0.36%) ⬆️
recharts/bundle-treeshaking-polar 448.06kB 4.98kB (1.12%) ⬆️
recharts/bundle-treeshaking-treemap* 354.73kB 5.55kB (1.59%) ⬆️
recharts/bundle-treeshaking-cartesian 643.63kB 4.87kB (0.76%) ⬆️

ℹ️ *Bundle size includes cached data from a previous commit

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
state/selectors/axisSelectors.js 3.18kB 59.24kB 5.68% ⚠️
chart/Treemap.js 2.27kB 30.65kB 7.98% ⚠️
state/selectors/tooltipSelectors.js 300 bytes 14.72kB 2.08%
chart/RechartsWrapper.js 332 bytes 13.61kB 2.5%
state/keyboardEventsMiddleware.js 424 bytes 7.24kB 6.22% ⚠️
component/DefaultTooltipContent.js 10 bytes 6.19kB 0.16%
state/selectors/pieSelectors.js 36 bytes 4.33kB 0.84%
state/selectors/polarSelectors.js 74 bytes 3.94kB 1.92%
state/selectors/dataSelectors.js 1.52kB 3.32kB 84.9% ⚠️
state/selectors/combiners/combineTooltipPayloadConfigurations.js 45 bytes 2.15kB 2.14%
state/selectors/combiners/combineConfiguredScale.js -32 bytes 1.56kB -2.01%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 1.95kB 547.75kB 0.36%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
state/selectors/axisSelectors.js 3.32kB 69.27kB 5.03% ⚠️
chart/Treemap.js 2.27kB 32.37kB 7.53% ⚠️
state/selectors/tooltipSelectors.js 309 bytes 18.22kB 1.73%
chart/RechartsWrapper.js 332 bytes 15.46kB 2.19%
state/keyboardEventsMiddleware.js 482 bytes 7.86kB 6.53% ⚠️
component/DefaultTooltipContent.js 10 bytes 7.33kB 0.14%
state/selectors/polarSelectors.js 52 bytes 4.94kB 1.06%
state/selectors/pieSelectors.js 36 bytes 4.65kB 0.78%
state/selectors/dataSelectors.js 1.82kB 4.23kB 75.35% ⚠️
state/selectors/combiners/combineConfiguredScale.js -32 bytes 2.44kB -1.3%
state/selectors/combiners/combineTooltipPayloadConfigurations.js 45 bytes 2.36kB 1.95%
view changes for bundle: recharts/bundle-treeshaking-polar

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 4.98kB 448.06kB 1.12%
view changes for bundle: recharts/bundle-treeshaking-treemap

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 5.55kB 354.73kB 1.59%
view changes for bundle: recharts/bundle-treeshaking-cartesian

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 4.87kB 643.63kB 0.76%

@codecov

codecov Bot commented Mar 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.68%. Comparing base (0fa4cc3) to head (a68023b).
⚠️ Report is 28 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7125      +/-   ##
==========================================
+ Coverage   89.61%   89.68%   +0.06%     
==========================================
  Files         535      537       +2     
  Lines       40315    40674     +359     
  Branches     5492     5548      +56     
==========================================
+ Hits        36130    36477     +347     
- Misses       4177     4189      +12     
  Partials        8        8              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PavelVanecek
PavelVanecek merged commit 9632f94 into recharts:main May 24, 2026
52 checks passed
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

Successfully merging this pull request may close these issues.

Tooltip doesn't receive typing info from createHorizontalChart

2 participants