Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dataIndex type check #1091

Merged
merged 12 commits into from
Mar 27, 2024
9 changes: 8 additions & 1 deletion docs/examples/aria.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
interface FieldType {
name?: string;
age?: string;
address?: string;
}

const columns: TableProps<FieldType>['columns'] = [
{
title: 'Name',
dataIndex: 'name',
Expand Down
7 changes: 2 additions & 5 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as React from 'react';
import type { DeepNamePath } from './namePathType';

/**
* ColumnType which applied in antd: https://ant.design/components/table-cn/#Column
Expand Down Expand Up @@ -66,11 +67,7 @@ export interface RenderedCell<RecordType> {

export type Direction = 'ltr' | 'rtl';

export type DataIndex<RecordType = any> =
| keyof RecordType
| string
| number
| readonly (string | number)[];
export type DataIndex<T = any> = DeepNamePath<T>;

export type CellEllipsisType = { showTitle?: boolean } | boolean;

Expand Down
32 changes: 32 additions & 0 deletions src/namePathType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type BaseNamePath = string | number | boolean | (string | number | boolean)[];
/**
* Store: The store type from `FormInstance<Store>`
* ParentNamePath: Auto generate by nest logic. Do not fill manually.
*/
export type DeepNamePath<
Store = any,
ParentNamePath extends any[] = [],
> = ParentNamePath['length'] extends 10
? never
: // Follow code is batch check if `Store` is base type
true extends (Store extends BaseNamePath ? true : false)
? ParentNamePath['length'] extends 0
? Store | BaseNamePath // Return `BaseNamePath` instead of array if `ParentNamePath` is empty
: Store extends any[]
? [...ParentNamePath, number] // Connect path
: never
: Store extends any[] // Check if `Store` is `any[]`
? // Connect path. e.g. { a: { b: string }[] }
// Get: [a] | [ a,number] | [ a ,number , b]
[...ParentNamePath, number] | DeepNamePath<Store[number], [...ParentNamePath, number]>
: keyof Store extends never // unknown
? Store
: {
// Convert `Store` to <key, value>. We mark key a `FieldKey`
[FieldKey in keyof Store]: Store[FieldKey] extends Function
? never
:
| (ParentNamePath['length'] extends 0 ? FieldKey : never) // If `ParentNamePath` is empty, it can use `FieldKey` without array path
| [...ParentNamePath, FieldKey] // Exist `ParentNamePath`, connect it
| DeepNamePath<Required<Store>[FieldKey], [...ParentNamePath, FieldKey]>; // If `Store[FieldKey]` is object
}[keyof Store];
6 changes: 3 additions & 3 deletions src/utils/valueUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ function toArray<T>(arr: T | readonly T[]): T[] {
return (Array.isArray(arr) ? arr : [arr]) as T[];
}

interface GetColumnKeyColumn {
export interface GetColumnKeyColumn<T = any> {
key?: Key;
dataIndex?: DataIndex;
dataIndex?: DataIndex<T>;
}

export function getColumnsKey(columns: readonly GetColumnKeyColumn[]) {
export function getColumnsKey<T = any>(columns: readonly GetColumnKeyColumn<T>[]) {
const columnKeys: React.Key[] = [];
const keys: Record<PropertyKey, boolean> = {};

Expand Down
Loading