Skip to content

Instantly share code, notes, and snippets.

@lvabarajithan
Last active May 24, 2023 14:47
Show Gist options
  • Save lvabarajithan/9bfb68ae5c8ec9f3200ea66937ce29a6 to your computer and use it in GitHub Desktop.
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
/**
* 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