Skip to content

Commit

Permalink
chore: update ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jul 29, 2023
1 parent 316ece4 commit a6d8cf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/react-dom": "^18.0.0",
"@umijs/fabric": "^2.5.2",
"@umijs/test": "^3.2.27",
"dayjs": "^1.11.9",
"dumi": "^1.1.0",
"enzyme": "^3.1.0",
"enzyme-adapter-react-16": "^1.0.2",
Expand All @@ -72,6 +73,7 @@
"father-build": "^1.18.6",
"gh-pages": "^3.1.0",
"jest": "^29.3.1",
"moment": "^2.29.4",
"np": "^5.0.3",
"prettier": "^2.1.2",
"react": "^16.14.0",
Expand Down
19 changes: 17 additions & 2 deletions src/namePathType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
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.
Expand All @@ -13,7 +21,8 @@ export type DeepNamePath<Store = any, ParentNamePath extends any[] = []> =
: 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<Store[number], [...ParentNamePath, number]>
| [...ParentNamePath, number]
| DeepNamePath<PureObject<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 @@ -23,7 +32,13 @@ 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<Store[FieldKey], [...ParentNamePath, FieldKey]> // If `Store[FieldKey]` is object
? DeepNamePath<PureObject<Store[FieldKey]>, [...ParentNamePath, FieldKey]> // If `Store[FieldKey]` is object
: never);
}[keyof Store]
: never;

type Test = {
date: Moment;
};

type CCC = DeepNamePath<Test>;

0 comments on commit a6d8cf9

Please sign in to comment.