Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,41 @@ export interface ColumnGroupType<RecordType> extends ColumnSharedType<RecordType

export type AlignType = 'start' | 'end' | 'left' | 'right' | 'center' | 'justify' | 'match-parent';

type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;

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)[]
>;

Comment on lines +120 to +126
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.

type Unwrap<TArr extends readonly (string | number)[]> = TArr extends { length: 0 }
? []
: number extends TArr['length']
? TArr
: (string | number)[] extends TArr ? TArr :
TArr
| (TArr extends { length: 1 }
? TArr
: TArr extends { length: 2 }
? [TArr[0]]
: TArr extends [...infer U, unknown]
? U extends readonly (string | number)[] ? Unwrap<U> : []
: []);

type DataIndexArrayType<RecordType> = IsExactlyAny<RecordType> extends true
? []
: RecordType extends string
? [number]
: RecordType extends Record<string | number, any>
? Unwrap<ExtractIndex<RecordType>>
: RecordType extends any[]
? [number, ...Unwrap<ExtractIndex<RecordType[number]>>]
: [];

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

Comment on lines +151 to +152
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.

export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
colSpan?: number;
dataIndex?: DataIndex<RecordType>;
Expand Down
Loading