@nestledjs/helpers
Version:
A collection of TypeScript utility types and helper functions for common development tasks
69 lines (68 loc) • 2.35 kB
TypeScript
/**
* A utility type that makes all properties of a type and its nested objects optional recursively.
* Useful for creating partial versions of complex nested objects.
*
* This version properly handles:
* - Arrays: Makes array elements deeply partial while preserving array structure
* - Functions: Leaves functions unchanged (they shouldn't be made partial)
* - Objects: Makes nested objects deeply partial
* - Primitives: Makes primitive values optional
*
* @template T - The type to make deeply partial
* @example
* ```typescript
* interface User {
* name: string
* hobbies: string[]
* friends: User[]
* onClick: () => void
* profile: {
* age: number
* address: {
* street: string
* city: string
* }
* }
* }
*
* type PartialUser = DeepPartial<User>
* // Result:
* // {
* // name?: string
* // hobbies?: string[]
* // friends?: DeepPartial<User>[]
* // onClick?: () => void
* // profile?: {
* // age?: number
* // address?: {
* // street?: string
* // city?: string
* // }
* // }
* // }
* ```
*/
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[] ? DeepPartial<U>[] : T[P] extends (...args: any[]) => any ? T[P] : T[P] extends object ? DeepPartial<T[P]> : T[P];
};
/**
* Pluralizes a word using the pluralize library, with special handling for words
* where the singular and plural forms are the same (like "data", "sheep").
* In those cases, appends "List" to make it clear it's a collection.
* Also includes overrides for known uncountable nouns that the pluralize library
* incorrectly pluralizes.
*
* @param name - The word to pluralize
* @returns The pluralized form, or the word + "List" if singular equals plural or is uncountable
*
* @example
* ```typescript
* getPluralName('user') // 'users'
* getPluralName('category') // 'categories'
* getPluralName('data') // 'dataList' (because 'data' plural is also 'data')
* getPluralName('sheep') // 'sheepList' (because 'sheep' plural is also 'sheep')
* getPluralName('luggage') // 'luggageList' (override: pluralize incorrectly returns 'luggages')
* getPluralName('furniture') // 'furnitureList' (override: pluralize incorrectly returns 'furnitures')
* ```
*/
export declare function getPluralName(name: string): string;