UNPKG

typedash

Version:

modern, type-safe collection of utility functions

24 lines (21 loc) 1 kB
import { A as AnyFunction } from './AnyFunction-DuIh5Fcc.cjs'; type RecordFunctions<T> = keyof { [K in keyof T as NonNullable<T[K]> extends AnyFunction ? K : never]: K; }; type NormalizedPath<Type, ExcludeFromType> = Path<NonNullable<Type>, Exclude<keyof NonNullable<Type>, ExcludeFromType>>; type OfType<D, E> = NonNullable<D> extends E ? true : false; type Path<T, K extends keyof T> = K extends string ? OfType<T[K], Record<string, any>> extends true ? OfType<T[K], ArrayLike<any>> extends true ? // eslint-disable-next-line @typescript-eslint/no-explicit-any K | `${K}.${NormalizedPath<T[K], keyof any[]>}` | `${K}[${number}]` : K | `${K}.${NormalizedPath<T[K], RecordFunctions<T[K]>>}` : K : never; /** * A string type that represents a valid path to a property in an object. * @example * ```ts * type ObjectPathExample = ObjectPath<{ * a: { * b: 1 * } * }>; 'a' | 'a.b' * ``` */ type ObjectPath<T> = string & (Path<T, keyof T> | keyof T); export type { ObjectPath as O };