UNPKG

@tmlmobilidade/utils

Version:

A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.

25 lines (24 loc) 1.78 kB
/** * Generates all possible dot-separated paths for a given object type, including paths through arrays. * Turns `{ a: { b: string }, arr: { c: number }[] }` into `"a" | "a.b" | "arr" | "arr.0" | "arr.0.c"` * @template T The object type to generate paths for. * @template Prev A helper type to accumulate the current path prefix. */ export type DotPath<T, Prev extends string = ''> = { [K in keyof T & string]: T[K] extends (infer U)[] ? `${Prev}${K}.${number}` | `${Prev}${K}` | DotPath<U, `${Prev}${K}.${number}.`> : T[K] extends Record<string, any> ? `${Prev}${K}` | DotPath<T[K], `${Prev}${K}.`> : `${Prev}${K}`; }[keyof T & string]; /** * Retrieves the type of the value located at a specified dot-separated path within an object type. * Supports paths through nested objects and arrays. * @template T The object type to retrieve the value from. * @template P The dot-separated path to the value. */ export type PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? T[Key] extends (infer U)[] ? Rest extends `${number}.${infer SubRest}` ? PathValue<U, SubRest> : Rest extends `${number}` ? U : never : PathValue<T[Key], Rest> : never : P extends keyof T ? T[P] : T extends (infer U)[] ? P extends `${number}` ? U : never : never; /** * Utility function that returns the value at a given path in an object. * @param obj The object to retrieve the value from. * @param path The path to the value in the object. * @returns The value at the given path or undefined if the path is invalid. */ export declare function getValueAtPath<T, P extends DotPath<T>>(obj: T, path: P): PathValue<T, P>; export declare function setValueAtPath<T extends object, P extends DotPath<T>>(obj: T, path: P, value: PathValue<T, P>): T;