Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
25e0d13
fix: fieldPath not support arrayIndex like a[1]/a.1
pointhalo Mar 4, 2025
2556545
test: more type test case
pointhalo Mar 5, 2025
58c4f54
Merge branch release into fix-fieldPath
pointhalo Mar 5, 2025
3d4a73b
v2.76.0-alpha.1
pointhalo Mar 5, 2025
2c331b7
chore: update root dependence typescript version from v4 -> 5.8.2
pointhalo Mar 5, 2025
d7ff92b
chore: tsconfig remove suppressImplicitAnyIndexErrors
pointhalo Mar 5, 2025
0009aac
chore: fix type error of rspack plugin for webcomponent loader
pointhalo Mar 10, 2025
fc92f7a
fix: Semantic generic parameters
pointhalo Mar 11, 2025
a168b16
fix: useFieldState type
pointhalo Mar 11, 2025
7bf3579
chore: Try to be compatible with strict type
pointhalo Mar 11, 2025
7bca589
v2.76.0-alpha.2
pointhalo Mar 11, 2025
f783c00
chore: ingore some case
pointhalo Mar 11, 2025
6187fa0
v2.76.0-alpha.3
pointhalo Mar 11, 2025
b45633c
fix: type error
pointhalo Mar 11, 2025
495c8a7
v2.76.0-alpha.4
pointhalo Mar 11, 2025
121ec35
fix: type error of form hoc and datepicker month
pointhalo Mar 11, 2025
5f8908b
chore: update alpha version hash
pointhalo Mar 11, 2025
ed20b57
v2.76.0-alpha.5
pointhalo Mar 11, 2025
7fc80df
fix: add Reg/Map/Set endpoint
pointhalo Mar 11, 2025
f788d9b
v2.76.0-alpha.6
pointhalo Mar 11, 2025
f30b478
v2.76.0-alpha.6
pointhalo Mar 11, 2025
caaa4cf
Merge branch 'fix-fieldPath' of github.com:DouyinFE/semi-design into …
pointhalo Mar 11, 2025
2b482e6
v2.76.0-alpha.7
pointhalo Mar 11, 2025
7e85604
fix: without generic
pointhalo Mar 11, 2025
daff828
v2.76.0-alpha.8
pointhalo Mar 11, 2025
4efe8f7
fix: remove isSpecailObject type define
pointhalo Mar 11, 2025
bed6f42
v2.76.0-alpha.9
pointhalo Mar 11, 2025
71ab518
fix: wrong version bump
pointhalo Mar 11, 2025
f4aa4ec
fix: wrong version bump
pointhalo Mar 11, 2025
9181267
fix: wrong version bump
pointhalo Mar 11, 2025
be57966
v2.76.0-alpha.9
pointhalo Mar 11, 2025
ebd0669
v2.76.0-alpha.10
pointhalo Mar 11, 2025
81f1818
Merge branch Main into fix-fieldPath
pointhalo Mar 11, 2025
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
Prev Previous commit
Next Next commit
fix: add Reg/Map/Set endpoint
  • Loading branch information
pointhalo committed Mar 11, 2025
commit 7fc80dfa0bb1c645ada3dded35599b686d50baa8
83 changes: 31 additions & 52 deletions packages/semi-foundation/form/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,6 @@ type ArrayIndexPath<K extends string | number, U> =
| `${K}.${number}` // 支持 array.index
| `${K}.${number}.${FieldPath<U>}`; // 支持 array.index.child

// FieldPath 类型定义,支持对象和数组字段路径
// export type FieldPath<T> = T extends Array<infer U>
// ? | `${number}` // 如果是数组,支持数字索引(如 `[0]`)
// | `${number}.${FieldPath<U>}` // 支持数组嵌套路径(如 `[0].field`)
// : T extends object
// ? {
// [K in keyof T]: K extends string
// ? T[K] extends Array<infer U> | object
// ? | `${K}`
// | `${K}.${FieldPath<T[K]>}`
// | ArrayIndexPath<K, U>
// : `${K}` // 只允许键路径
// : never;
// }[keyof T]
// : never;

// FieldPath 类型定义,支持对象和数组字段路径
export type FieldPath<T> = T extends Array<infer U>
? `${number}` | `${number}.${FieldPath<Exclude<U, undefined>>}`
: T extends object ? {
[K in keyof T]: K extends string
? Exclude<T[K], undefined> extends Date
? `${K}`
: T[K] extends Array<infer U> | object | undefined
? `${K}` |
`${K}.${FieldPath<T[K]>}` |
ArrayIndexPath<K, U>
: `${K}`
: never;
}[keyof T]
: never;


export type FieldPathValue<T, P extends string> =
ArrayFieldPathValue<T, P> |
(P extends `${infer K}.${infer Rest}`
? K extends keyof T
? FieldPathValue<Exclude<T[K], undefined>, Rest> // 添加 undefined过滤,避免用户 tsconfig strictNullChecks 为 true 时,无法正确推断
: never
: P extends keyof T
? T[P]
: never);

type ArrayFieldPathValue<T, P extends string> =
P extends `${infer K}[${infer I}]${infer Rest}`
? K extends keyof T
Expand All @@ -109,16 +66,38 @@ type ArrayFieldPathValue<T, P extends string> =
: never
: never;

// 判断是否是特殊类型(如 Date、Set、Map、RegExp 等, 无需继续推导)
type IsSpecialObject<T> =
T extends Date | RegExp | Map<any, any> | Set<any>
? true
: false;

// FieldPath 类型定义,支持对象和数组字段路径,支持数字索引(如 `[0]`),支持数组嵌套路径(如 `[0].field`)
export type FieldPath<T> = T extends Array<infer U>
? `${number}` | `${number}.${FieldPath<NonNullable<U>>}`
: T extends object
? {
[K in keyof T]: K extends string
// ? Exclude<T[K], undefined> extends Date
? IsSpecialObject<NonNullable<T[K]>> extends true
? `${K}` // 如果是特殊类型(如 Date、Set 等),只允许顶层键路径
: T[K] extends Array<infer U> | object | undefined
? `${K}` | `${K}.${FieldPath<Required<T[K]>>}` |ArrayIndexPath<K, U> // 嵌套处理
: `${K}`
: never;
}[keyof T]
: never;

// FieldPathValue 类型定义,支持从路径字符串中推导数组和对象的值
// export type FieldPathValue<T, P extends string> =
// ArrayFieldPathValue<T, P> // 处理数组路径
// | (P extends `${infer K}.${infer Rest}`
// ? K extends keyof T
// ? FieldPathValue<Exclude<T[K], undefined>, Rest> // 添加 undefined过滤,避免用户 tsconfig strictNullChecks 为 true 时,无法正确推断
// : never
// : P extends keyof T
// ? T[P]
// : never);
export type FieldPathValue<T, P extends string> =
ArrayFieldPathValue<T, P> |
(P extends `${infer K}.${infer Rest}`
? K extends keyof T
? FieldPathValue<NonNullable<T[K]>, Rest> // 添加 undefined过滤,避免用户 tsconfig strictNullChecks 为 true 时,无法正确推断
: never
: P extends keyof T
? T[P]
: never);

// use object replace Record<string, any>, fix issue 933
export interface BaseFormApi<FormValuesType extends object = any> {
Expand Down
7 changes: 6 additions & 1 deletion packages/semi-ui/form/_story/form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ interface FData {
R32: number;
R33: boolean;
}>;
}
},
testS: Set<string>;
testT: Map<string, number>;
}
};
};
Expand All @@ -158,6 +160,9 @@ const FCDemo = () => {
let year = testO?.getFullYear();
let testP = formApi.getValue('optional.testM.testN.testP');
testP = testP + 1;
let testSet = formApi.getValue('optional.testM.testN.testS');
let testMap = formApi.getValue('optional.testM.testN.testT');

let NotExist = formApi.getValue('optional.testM.testN.NotExist');

// ✅ 应该合法的, 注意,setValue只对 fieldPath做校验,对 value 不做严格校验
Expand Down