/** * Deep setting * * Derived from @url https://github.com/react-hook-form/react-hook-form/tree/011fad503cc8d4543892f8e847b9bd58c1d9400f/src/types/path * */ import type { ArrayKey, BrowserNativeObject, IsAny, IsEqual, IsTuple, Primitive, TupleKeys } from './common.js'; /** * Helper function to break apart T1 and check if any are equal to T2 * * See {@link IsEqual} */ type AnyIsEqual = T1 extends T2 ? (IsEqual extends true ? true : never) : never; /** * Helper type for recursively constructing paths through a type. * This actually constructs the strings and recurses into nested * object types. * * See {@link Path} */ type PathImpl = V extends Primitive | BrowserNativeObject ? `${K}` : true extends AnyIsEqual ? `${K}` : `${K}` | `${K}.${PathInternal}`; /** * Helper type for recursively constructing paths through a type. * This obscures the internal type param TraversedTypes from exported contract. * * See {@link Path} */ type PathInternal = T extends ReadonlyArray ? IsTuple extends true ? { [K in TupleKeys]-?: PathImpl; }[TupleKeys] : PathImpl : { [K in keyof T]-?: PathImpl; }[keyof T]; /** * Type which eagerly collects all paths through a type * @typeParam T - type which should be introspected * @example * ``` * Path<{foo: {bar: string}}> = 'foo' | 'foo.bar' * ``` */ type Path = T extends any ? PathInternal : never; /** * Helper type for recursively constructing paths through a type. * This actually constructs the strings and recurses into nested * object types. * * See {@link ArrayPath} */ type ArrayPathImpl = V extends Primitive | BrowserNativeObject ? IsAny extends true ? string : never : V extends ReadonlyArray ? U extends Primitive | BrowserNativeObject ? IsAny extends true ? string : never : true extends AnyIsEqual ? never : `${K}` | `${K}.${ArrayPathInternal}` : true extends AnyIsEqual ? never : `${K}.${ArrayPathInternal}`; /** * Helper type for recursively constructing paths through a type. * This obscures the internal type param TraversedTypes from exported contract. * * See {@link ArrayPath} */ type ArrayPathInternal = T extends ReadonlyArray ? IsTuple extends true ? { [K in TupleKeys]-?: ArrayPathImpl; }[TupleKeys] : ArrayPathImpl : { [K in keyof T]-?: ArrayPathImpl; }[keyof T]; /** * Type which eagerly collects all paths through a type which point to an array * type. * @typeParam T - type which should be introspected. * @example * ``` * Path<{foo: {bar: string[], baz: number[]}}> = 'foo.bar' | 'foo.baz' * ``` */ type ArrayPath = T extends any ? ArrayPathInternal : never; /** * Type to evaluate the type which the given path points to. * @typeParam T - deeply nested type which is indexed by the path * @typeParam P - path into the deeply nested type * @example * ``` * PathValue<{foo: {bar: string}}, 'foo.bar'> = string * PathValue<[number, string], '1'> = string * ``` */ type PathValue | ArrayPath> = T extends any ? P extends keyof T ? T[P] : P extends `${infer K}.${infer R}` ? K extends keyof T ? R extends Path ? PathValue : never : K extends `${ArrayKey}` ? T extends ReadonlyArray ? PathValue> : never : never : P extends `${ArrayKey}` ? T extends ReadonlyArray ? V : never : never : never; /** * Set a nested value from an object using a dot separated path. * * Basic Path: `'foo.bar'` * * With Array: `'foo.1.bar'` */ export declare function setNestedValue>(obj: T, path: P, value: PathValue): T; /** * Get a nested value from an object using a dot separated path. * * Basic Path: `'foo.bar'` * * With Array: `'foo.1.bar'` */ export declare function getNestedValue>(obj: T, path: P): any; /** Export the path and path value types to create other setter functions */ export type { Path, PathValue }; //# sourceMappingURL=nested.d.ts.map