Skip to content

fix: coerce accessor key to string during column creation#5808

Closed
schardev wants to merge 2 commits into
TanStack:mainfrom
schardev:fix-accessor-key
Closed

fix: coerce accessor key to string during column creation#5808
schardev wants to merge 2 commits into
TanStack:mainfrom
schardev:fix-accessor-key

Conversation

@schardev

@schardev schardev commented Nov 19, 2024

Copy link
Copy Markdown

This is based on the PR by @jpedroh (#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 #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.

@changeset-bot

changeset-bot Bot commented Nov 10, 2025

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 329cbce

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

@coderabbitai

coderabbitai Bot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

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 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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/table-core/src/core/column.ts (1)

115-118: Use normalized accessorKey for consistency.

Line 117 references resolvedColumnDef.accessorKey directly, which could be a number. While JavaScript treats arr[0] and arr["0"] equivalently, this is inconsistent with lines 85-88, 100, 104, and 108, which all use the normalized accessorKey variable.

Apply this diff to use the normalized variable:

    } else {
      accessorFn = (originalRow: TData) =>
-        (originalRow as any)[resolvedColumnDef.accessorKey]
+        (originalRow as any)[accessorKey]
    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 02c203a and 329cbce.

📒 Files selected for processing (3)
  • packages/react-table/tests/core/core.test.tsx (2 hunks)
  • packages/table-core/src/core/column.ts (1 hunks)
  • packages/table-core/src/types.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/react-table/tests/core/core.test.tsx (3)
packages/table-core/src/types.ts (2)
  • Table (137-152)
  • ColumnDef (328-331)
packages/react-table/src/index.tsx (1)
  • useReactTable (57-94)
packages/table-core/src/utils/getCoreRowModel.ts (1)
  • getCoreRowModel (5-82)
🔇 Additional comments (4)
packages/table-core/src/types.ts (1)

337-337: LGTM! Type change enables numeric accessorKey values.

The broadening of accessorKey to accept string | number correctly supports the use case of numeric array indices. This aligns well with the normalization logic in column.ts that coerces numeric keys to strings.

packages/table-core/src/core/column.ts (1)

78-81: LGTM! Normalization logic correctly handles numeric accessorKey.

The coercion to string is implemented safely and handles both defined and undefined cases appropriately. This ensures that downstream code using string methods (replace, includes) won't encounter runtime errors.

packages/react-table/tests/core/core.test.tsx (2)

1-1: LGTM! Import enables testing library matchers.

The vitest-compatible jest-dom import is necessary for the .toBeInTheDocument() matcher used in the new test.


268-328: LGTM! Comprehensive test coverage for numeric accessorKey.

The test effectively validates the fix for issue #4815 by:

  • Using tuple array data with numeric indices (0, 1, 2) as accessorKey values
  • Verifying headers render correctly with the expected text
  • Verifying body content renders correctly with actual data values

This provides solid coverage for the numeric accessorKey use case.

@schardev

Copy link
Copy Markdown
Author

Fixed by: #6434

@schardev schardev closed this Jul 18, 2026
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.

Array index as an accessorKey in ColumnDef does not work

1 participant