table icon indicating copy to clipboard operation
table copied to clipboard

fix: coerce accessor key to string during column creation

Open schardev opened this issue 1 year ago โ€ข 2 comments

This is based on the PR by @jpedroh (https://github.com/TanStack/table/pull/4835)

During render, if the accessorKey was not a string, e.g. a number, it would result in an error when trying to normalize it during column creation, because it was trying to access methods which are not available in a number (replace and include). This PR forces the accessor key to be constructed as a string during column creation, thus removing this bug.

fixes https://github.com/TanStack/table/issues/4815

Summary by CodeRabbit

Release Notes

New Features

  • Columns now accept numeric array indices as accessor keys. Tables can be configured using integer indices (0, 1, 2, etc.) alongside traditional string-based keys, providing enhanced flexibility when working with array-based data structures.

schardev avatar Nov 19 '24 10:11 schardev

โš ๏ธ No Changeset found

Latest commit: 329cbce1f4250a717a54144ed28630b252b7c771

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Nov 10 '25 09:11 changeset-bot[bot]

Walkthrough

This PR enables support for numeric array indices as accessorKey values in column definitions. The changes update the type signature to accept number in addition to string, implement normalization logic to coerce numeric keys to strings internally, and add test coverage validating the functionality.

Changes

Cohort / File(s) Change Summary
Type definition update
packages/table-core/src/types.ts
Updates ColumnDefResolved.accessorKey property from string to string | number to allow numeric array indices
Accessor key normalization
packages/table-core/src/core/column.ts
Implements conditional coercion of resolvedColumnDef.accessorKey to string when defined, enabling numeric keys to work with downstream accessor logic
Test coverage
packages/react-table/tests/core/core.test.tsx
Adds import for '@testing-library/jest-dom/vitest' and new test case verifying numeric indices (0, 1, 2) as accessorKey values render headers and data correctly

Estimated code review effort

๐ŸŽฏ 2 (Simple) | โฑ๏ธ ~10 minutes

  • Focus areas for review:
    • Verify the string coercion logic in column.ts handles all edge cases (undefined, null, numeric values)
    • Confirm the test case properly validates the documented array-index accessor behavior
    • Ensure type broadening to string | number doesn't introduce unexpected behavior downstream

Poem

๐Ÿฐ Numeric keys once caused a fright, But now they're coerced to strings just right! Arrays indexed, clean and true, Your table renders fresh and new! ๐ŸŽ‰

Pre-merge checks and finishing touches

โŒ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check โš ๏ธ Warning The PR description references the bug fix and linked issue but does not follow the required template structure with sections for 'Changes', 'Checklist', and 'Release Impact'. Provide a description following the template with '๐ŸŽฏ Changes' section, 'โœ… Checklist' with contribution verification, and '๐Ÿš€ Release Impact' with changeset information.
Docstring Coverage โš ๏ธ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
โœ… Passed checks (3 passed)
Check name Status Explanation
Title check โœ… Passed The title clearly and concisely describes the main change: coercing accessor keys to strings during column creation, which directly addresses the bug fix.
Linked Issues check โœ… Passed The PR successfully addresses issue #4815 by coercing numeric accessorKey values to strings, allowing array indices to work without crashing.
Out of Scope Changes check โœ… Passed All changes (type updates, string coercion in column creation, and test coverage) are directly scoped to fixing the array-index accessorKey bug.
โœจ Finishing touches
  • [ ] ๐Ÿ“ Generate docstrings
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

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[bot] avatar Nov 10 '25 09:11 coderabbitai[bot]