react-component / react-component/table

make `rowKey` more type safe

Open
#748 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1.4k
Forks
618
Avg merge
10h 19m
Merged PRs (30d)
2

Description

Describe the problem

https://github.com/react-component/table/blob/e23e391f4fc1fc884814c853fda580349024f783/src/Table.tsx#L119

From the source code, currently the type of rowKey attribute is string or callback function. However I think we can make the type of rowKey more precise.



What I suggest

Before
rowKey?: string | GetRowKey<RecordType>;
After
rowKey?: keyof RecordType | GetRowKey<RecordType>;

Because the rowKey attribute means that use dataSource[i][rowKey] as the key attribute that react wants.

This will make the type more precise. It will be nice that typescript can check if we have given the correct value to rowKey attribute.



Expected result

Before
const data = [{ id: 1, name: 'Tom' }, {id: 2, name: 'Bob' }];

return <Table dataSource={data} rowKey="username" />; // no error 😕 
After
const data = [{ id: 1, name: 'Tom' }, {id: 2, name: 'Bob' }];

return <Table dataSource={data} rowKey="username" />; // TS ERROR!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/Table.tsx around line 119 and inspect the surrounding rowKey and GetRowKey type definitions. Verify the proposed typing against the dataSource examples, including rejecting a rowKey that is not a key of the record type, while preserving callback support.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.