fix: Better type definition for dataIndex#753
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/react-component/table/ATW6XKoFv29uFA9TrJyzMhLFEcqT |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
|
CI failed. |
Fixing it |
|
定义出来一个类型吧 |
|
This pull request introduces 1 alert when merging e24588a into 9d4b3e4 - view on LGTM.com new alerts:
|
嵌套对象不管了……
Done |
|
这CI时灵时不灵…… 懂了……要手动触发……我还是本地试吧 |
|
现在过不了应该是ts本身的bug吧 |
Walkthrough在 src/interface.ts 引入若干内部 TypeScript 类型辅助(IsExactlyAny、ExtractIndex、Unwrap、DataIndexArrayType、DataIndexType)以推导复杂 RecordType 的索引路径;未导出、无运行时代码改动;对外接口(如 ColumnType 与其 dataIndex 声明)保持不变。 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Assessment against linked issues
Poem
✨ Finishing Touches
🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
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.
📒 Files selected for processing (1)
src/interface.ts(1 hunks)
🔇 Additional comments (1)
src/interface.ts (1)
118-118:IsExactlyAny的实现可接受该类型守卫写法常见且能正确识别
any。无阻塞问题。
| 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)[] | ||
| >; | ||
|
|
There was a problem hiding this comment.
缺少 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.
| 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.
| type DataIndexType<RecordType> = Readonly<DataIndexArrayType<RecordType>> extends { length: 0 } ? DataIndex : Readonly<DataIndexArrayType<RecordType>> | Readonly<DataIndexArrayType<RecordType>>[0] | ||
|
|
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
DataIndexType 回退时遗漏泛型参数且未被 ColumnType 使用,当前改动“无效”
- 回退到
DataIndex时未传入RecordType,等价于退化为DataIndex<any>,丢失泛型约束。 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.
Fixes #435 #587 #532 #717
Summary by CodeRabbit