Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Jul 29, 2023
1 parent a6d8cf9 commit 173dd7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/namePathType.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import type { Moment } from 'moment';
import type { Dayjs } from 'dayjs';

/**
* Remove type from Object
*/
type PureObject<T> = Omit<T, keyof Object>;

/**
* 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[] = []> =
export type DeepNamePathBase<Store = any, ParentNamePath extends any[] = []> =
// Follow code is batch check if `Store` is base type
Store extends string | number
? string | number
? Store
: string[] extends Store
? Store
: number[] extends Store
? Store
: Store extends Record<string, any>[] // Check if `Store` is `object[]`
? // Connect path. e.g. { a: { b: string }[] }
// Get: [a] | [ a,number] | [ a ,number , b]
| [...ParentNamePath, number]
| DeepNamePath<PureObject<Store[number]>, [...ParentNamePath, number]>
| []
| [...ParentNamePath, number]
| DeepNamePathBase<Store[number], [...ParentNamePath, number]>
: Store extends Record<string, any> // Check if `Store` is `object`
? {
// Convert `Store` to <key, value>. We mark key a `FieldKey`
Expand All @@ -32,13 +25,15 @@ export type DeepNamePath<Store = any, ParentNamePath extends any[] = []> =
| (Store[FieldKey] extends (number | string)[]
? [...ParentNamePath, FieldKey, number] // If `Store[FieldKey]` is base array type
: Store[FieldKey] extends Record<string, any>
? DeepNamePath<PureObject<Store[FieldKey]>, [...ParentNamePath, FieldKey]> // If `Store[FieldKey]` is object
? DeepNamePathBase<Store[FieldKey], [...ParentNamePath, FieldKey]> // If `Store[FieldKey]` is object
: never);
}[keyof Store]
: never;

type Test = {
date: Moment;
type IsOptional<Key extends keyof Obj, Obj> = {} extends Pick<Obj, Key> ? Key : never;

export type DeepRequired<T = any> = {
[Key in keyof T]-?: IsOptional<Key, T> extends never ? T[Key] : DeepRequired<T[Key]>;
};

type CCC = DeepNamePath<Test>;
export type DeepNamePath<T = any> = DeepNamePathBase<DeepRequired<T>>;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strictNullChecks": true,
"paths": {
"@/*": ["src/*"],
"@@/*": ["src/.umi/*"],
Expand Down

0 comments on commit 173dd7a

Please sign in to comment.