-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): move Flatten type into the module
- Loading branch information
Showing
3 changed files
with
46 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @module Types | ||
*/ | ||
|
||
/** | ||
* Type subjective to flattening. | ||
*/ | ||
export type Flattenable = Record<string, unknown> | any[] | ||
|
||
/** | ||
* Recursively flattens object type to a union of its keys | ||
* combined through dot-notation. | ||
*/ | ||
export type Flatten< | ||
Source extends Record<string, unknown> | any[], | ||
> = keyof Source extends keyof any[] | ||
? `${number}` | ||
: ( | ||
| keyof Source | ||
| keyof { | ||
[ | ||
Key in keyof Source as | ||
Key extends string | ||
? Source[Key] extends Record<string, unknown> | ||
? `${Key}.${Flatten<Source[Key]>}` | ||
: Source[Key] extends any[] | ||
? Source[Key] extends Array<any[]> | Record<string, unknown>[] | ||
? `${Key}.${number}` | `${Key}.${number}.${Flatten<Source[Key][number]>}` | ||
: `${Key}.${number}` | ||
: Key | ||
: never | ||
] : never | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters