Skip to content

fix: Better type definition for dataIndex#753

Open
tychenjiajun wants to merge 17 commits into
react-component:masterfrom
tychenjiajun:master
Open

fix: Better type definition for dataIndex#753
tychenjiajun wants to merge 17 commits into
react-component:masterfrom
tychenjiajun:master

Conversation

@tychenjiajun
Copy link
Copy Markdown

@tychenjiajun tychenjiajun commented Feb 17, 2022

Fixes #435 #587 #532 #717

Summary by CodeRabbit

  • 重构
    • 优化内部 TypeScript 类型辅助以提升复杂数据索引的类型推断与安全性,无运行时改动。
  • 其他
    • 无公开 API 变更;现有功能、界面与用法不受影响。

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 17, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/react-component/table/ATW6XKoFv29uFA9TrJyzMhLFEcqT
✅ Preview: https://table-git-fork-tychenjiajun-master-react-component.vercel.app

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 17, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.38%. Comparing base (9d4b3e4) to head (e9c5eec).
Report is 245 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #753   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          34       35    +1     
  Lines         976      976           
  Branches      281      281           
=======================================
  Hits          970      970           
  Misses          6        6           

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

@yoyo837
Copy link
Copy Markdown
Member

yoyo837 commented Feb 17, 2022

CI failed.

@tychenjiajun
Copy link
Copy Markdown
Author

CI failed.

Fixing it

@tychenjiajun tychenjiajun changed the title fix: Better type definition for dataIndex WIP: fix: Better type definition for dataIndex Feb 17, 2022
@yoyo837
Copy link
Copy Markdown
Member

yoyo837 commented Feb 17, 2022

定义出来一个类型吧

@lgtm-com
Copy link
Copy Markdown
Contributor

lgtm-com Bot commented Feb 17, 2022

This pull request introduces 1 alert when merging e24588a into 9d4b3e4 - view on LGTM.com

new alerts:

  • 1 for Syntax error

嵌套对象不管了……
@tychenjiajun tychenjiajun changed the title WIP: fix: Better type definition for dataIndex fix: Better type definition for dataIndex Feb 17, 2022
@tychenjiajun tychenjiajun changed the title fix: Better type definition for dataIndex WIP: fix: Better type definition for dataIndex Feb 17, 2022
@tychenjiajun tychenjiajun changed the title WIP: fix: Better type definition for dataIndex fix: Better type definition for dataIndex Feb 17, 2022
@tychenjiajun tychenjiajun changed the title fix: Better type definition for dataIndex WIP: fix: Better type definition for dataIndex Feb 17, 2022
@tychenjiajun tychenjiajun changed the title WIP: fix: Better type definition for dataIndex fix: Better type definition for dataIndex Feb 17, 2022
@tychenjiajun
Copy link
Copy Markdown
Author

定义出来一个类型吧

Done

@tychenjiajun
Copy link
Copy Markdown
Author

tychenjiajun commented Feb 20, 2022

这CI时灵时不灵…… 懂了……要手动触发……我还是本地试吧

@tychenjiajun
Copy link
Copy Markdown
Author

tychenjiajun commented Feb 20, 2022

现在过不了应该是ts本身的bug吧

@tychenjiajun
Copy link
Copy Markdown
Author

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Sep 2, 2025

Walkthrough

在 src/interface.ts 引入若干内部 TypeScript 类型辅助(IsExactlyAny、ExtractIndex、Unwrap、DataIndexArrayType、DataIndexType)以推导复杂 RecordType 的索引路径;未导出、无运行时代码改动;对外接口(如 ColumnType 与其 dataIndex 声明)保持不变。

Changes

Cohort / File(s) Summary
类型系统内部增强:DataIndex 推导
src/interface.ts
新增内部类型工具 IsExactlyAny、ExtractIndex、Unwrap、DataIndexArrayType、DataIndexType;用于细化基于 RecordType 的索引推导。未对外导出,ColumnType 与 dataIndex 的公开声明未变,更无运行时变更。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
使用泛型 DataIndex 以基于 keyof 约束 dataIndex(#435 未修改导出的 DataIndex 为泛型;ColumnType 的 dataIndex 仍使用现有非泛型类型。

Poem

我是码田里的小兔子,蹦跳查类型的枝,
把 any 轻轻剥离,把索引路径细织🧵,
不动公开 API,却让内里更精致,
等到时机合适,再把泛型旗帜高举,
月光下,类型树沙沙作诗。 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link
Copy Markdown

vercel Bot commented Sep 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
table Ready Ready Preview Comment Sep 2, 2025 3:06pm

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/interface.ts (2)

127-140: Unwrap 可读性偏弱,建议补充注释或拆小步骤

当前联合类型展开逻辑正确但较难读,建议加简短注释(如“生成所有前缀路径”),或将分支拆成若干中间条件类型,便于后续维护。


141-150: 仅推荐:为只读数组/tuple 添加分支以支持未来使用 当前仓库未发现 Readonly 数组/tuple 作为 RecordType 的场景,若后续需要可将

RecordType extends any[]

修改为

RecordType extends readonly any[]

以覆盖只读数组和 tuple。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 688d72a and 10db7a1.

📒 Files selected for processing (1)
  • src/interface.ts (1 hunks)
🔇 Additional comments (1)
src/interface.ts (1)

118-118: IsExactlyAny 的实现可接受

该类型守卫写法常见且能正确识别 any。无阻塞问题。

Comment thread src/interface.ts
Comment on lines +120 to +126
type ExtractIndex<RecordType> = Extract<
{
[key in Extract<keyof RecordType, string | number>]: key extends never ? [] : IsExactlyAny<RecordType[key]> extends true ? [key, ...DataIndexArray] : [key, ...DataIndexArrayType<RecordType[key]>];
}[Extract<keyof RecordType, string | number>],
readonly (string | number)[]
>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

缺少 DataIndexArray 类型声明,导致类型报错

此处使用了未定义的 DataIndexArray(Line 122)。TypeScript 会直接报 “Cannot find name 'DataIndexArray'”,CI 失败大概率源于此。

建议新增通用别名,表达“后续可以跟任意路径段”的含义:

 type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
+type DataIndexArray = readonly (string | number)[];
 
 type ExtractIndex<RecordType> = Extract<
   {
-    [key in Extract<keyof RecordType, string | number>]: key extends never ? [] : IsExactlyAny<RecordType[key]> extends true ? [key, ...DataIndexArray] : [key, ...DataIndexArrayType<RecordType[key]>];
+    [key in Extract<keyof RecordType, string | number>]:
+      key extends never
+        ? []
+        : IsExactlyAny<RecordType[key]> extends true
+          ? [key, ...DataIndexArray]
+          : [key, ...DataIndexArrayType<RecordType[key]>];
   }[Extract<keyof RecordType, string | number>],
   readonly (string | number)[]
 >;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
type ExtractIndex<RecordType> = Extract<
{
[key in Extract<keyof RecordType, string | number>]: key extends never ? [] : IsExactlyAny<RecordType[key]> extends true ? [key, ...DataIndexArray] : [key, ...DataIndexArrayType<RecordType[key]>];
}[Extract<keyof RecordType, string | number>],
readonly (string | number)[]
>;
type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
type DataIndexArray = readonly (string | number)[];
type ExtractIndex<RecordType> = Extract<
{
[key in Extract<keyof RecordType, string | number>]:
key extends never
? []
: IsExactlyAny<RecordType[key]> extends true
? [key, ...DataIndexArray]
: [key, ...DataIndexArrayType<RecordType[key]>];
}[Extract<keyof RecordType, string | number>],
readonly (string | number)[]
>;
🤖 Prompt for AI Agents
In src/interface.ts around lines 120 to 126, the type alias DataIndexArray is
referenced but not declared causing TS "Cannot find name 'DataIndexArray'"; add
a new exported type alias above this block such as a generic readonly array of
path segments (e.g., DataIndexArray representing subsequent path segments as
readonly (string | number)[]), and ensure any related usages/imports align with
this name so the ExtractIndex type compiles correctly.

Comment thread src/interface.ts
Comment on lines +151 to +152
type DataIndexType<RecordType> = Readonly<DataIndexArrayType<RecordType>> extends { length: 0 } ? DataIndex : Readonly<DataIndexArrayType<RecordType>> | Readonly<DataIndexArrayType<RecordType>>[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

DataIndexType 回退时遗漏泛型参数且未被 ColumnType 使用,当前改动“无效”

  1. 回退到 DataIndex 时未传入 RecordType,等价于退化为 DataIndex<any>,丢失泛型约束。
  2. ColumnType<RecordType> 仍然使用 dataIndex?: DataIndex<RecordType>,导致新增推导类型完全未生效。

请修正泛型并将 ColumnType 切换为新类型:

-type DataIndexType<RecordType> = Readonly<DataIndexArrayType<RecordType>> extends { length: 0 } ? DataIndex : Readonly<DataIndexArrayType<RecordType>> | Readonly<DataIndexArrayType<RecordType>>[0]
+type DataIndexType<RecordType> =
+  Readonly<DataIndexArrayType<RecordType>> extends { length: 0 }
+    ? DataIndex<RecordType>
+    : Readonly<DataIndexArrayType<RecordType>> | Readonly<DataIndexArrayType<RecordType>>[0]

并在 ColumnType 中应用(在本文件稍后位置):

-  dataIndex?: DataIndex<RecordType>;
+  dataIndex?: DataIndexType<RecordType>;

这两处合并后,才能达到 PR 目标:对 dataIndex 提供基于 RecordType 的完整类型校验,非法键将产生 TS 报错。

验证建议:


🏁 Script executed:

#!/usr/bin/env bash
# 1) 检查 DataIndexArray 是否已定义
rg -nP 'type\s+DataIndexArray\b|[^A-Za-z]DataIndexArray\b' -C2

# 2) 确认 ColumnType 已切换到 DataIndexType
rg -nP 'interface\s+ColumnType<[^>]+>[\s\S]*?\bdataIndex\?\s*:\s*DataIndexType<'

# 3) 通过示例用例快速验证(需本地 tsc):应当对不存在的 key 报错
# 示例:
# type User = { id: number; name: string; addr: { city: string } };
# const cols: ColumnsType<User> = [{ dataIndex: ['addr', 'city'] }, { dataIndex: 'notExists' }]; // 第二项应报错

Length of output: 588


修复 DataIndexType 泛型和 ColumnType 中的引用

  • src/interface.ts 中,DataIndexType<RecordType> 的回退分支应使用 DataIndex<RecordType>,否则会退化为 DataIndex<any>,丢失泛型约束。
  • ColumnType<RecordType> 接口里的 dataIndex?: DataIndex<RecordType> 切换为 dataIndex?: DataIndexType<RecordType>,以确保非法键能在编译时报错。
🤖 Prompt for AI Agents
In src/interface.ts around lines 151-152, the conditional type fallback
currently uses DataIndex (non-generic) which collapses to DataIndex<any> and
loses the RecordType constraint; change the fallback to DataIndex<RecordType>.
Also update the ColumnType<RecordType> interface so its dataIndex property uses
DataIndexType<RecordType> instead of DataIndex<RecordType>, ensuring invalid
keys are caught at compile time; adjust both type references accordingly.

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.

Use DataIndex<RecordType> for better constraints?

3 participants