Last active
May 24, 2023 14:47
-
-
Save lvabarajithan/9bfb68ae5c8ec9f3200ea66937ce29a6 to your computer and use it in GitHub Desktop.
Typescript - Prettify intelli-sense, Make certain fields from type T as partial & others as required and group-by array elements type
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
/** | |
* Prettify the hover output of a type | |
*/ | |
export type Prettify<T> = { | |
[K in keyof T]: T[K] | |
} & {} | |
/** | |
* Make certain fields from T as partial | |
*/ | |
export type PartialCertain<T, K extends keyof T> = Prettify<Required<Omit<T, K>> & Partial<Pick<T, K>>> | |
/** | |
* Make a string as plural by adding a suffix | |
*/ | |
export type Pluralize<S extends string> = `${S}s`; | |
/** | |
* Group an array by it's elements | |
*/ | |
export type GroupByArray<T extends any[]> = Prettify<{ | |
[K in keyof T[number] as Pluralize<string & K>]: Extract<T[number], { [P in K]: T[K] }>[K][] | |
}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment