import type { Prop, Paths } from "../typings/types"; type If = B extends true ? F : S; type Or = B1 extends true ? true : B2; type Not = B extends true ? false : true; type Has = [U1] extends [U] ? true : false; type IsExactKey = string extends T ? false : number extends T ? false : symbol extends T ? false : true; type ValueByPath = P extends readonly [infer F, ...(infer R)] ? Or, Has> extends infer HasNullOrUndefined ? Exclude extends infer O2 ? O2 extends object ? F extends keyof O2 ? R extends [] ? If, O2[F] | undefined, O2[F]> : ValueByPath, /** * In case we run into some kind of dynamic dictionary * something like Record or Record * We want to make sure that we get T | undefined instead of T as a result */ Not>>> : undefined : never : never : never : never; export interface Path { /** * Retrieve the value at a given path. * **Note:** Use `as const` cast on the `paths` for type inference. * * @param {[String]} paths The path to use. * @param {Object} obj The object to retrieve the nested property from. * @return {*} The data at `path`. * @example * * path(['a', 'b'], {a: {b: 2}}); //=> 2 * path(['a', 'b'], {c: {b: 2}}); //=> undefined */ (pathToProp: Prop[], obj: object): unknown; /** * Retrieve the value at a given path. * **Note:** Use `as const` cast on the `paths` for type inference. * * @param {[String]} paths The path to use. * @return {*} function to get data at `path` for a given object * @example * * path(['a', 'b'])({a: {b: 2}}); //=> 2 * path(['a', 'b'])({c: {b: 2}}); //=> undefined */ (pathToProp: Prop[]): (obj: object) => unknown; /** * @deprecated * Please use `path` without type parameters instead. * Make sure to use `as const` cast for props array for type inference * @example * * path(['a', 'b'] as const, { a: { b: 2 } }); */ (pathToProp: Prop[], obj: object): T | undefined; /** * @deprecated * Please use `path` without type parameters instead. * Make sure to use `as const` cast for props array for type inference * @example * * path(['a', 'b'] as const)({ a: { b: 2 } }); */ (pathToProp: Prop[]): (obj: object) => T | undefined; /** * Retrieve the value at a given path. * * @param {[String]} paths The path to use. * @param {Object} obj The object to retrieve the nested property from. * @return {*} The data at `path`. * @example * * const johnDoe = { * fio: { * firstName: 'John', * lastName: 'Doe', * }, * }; * const firstName = path(['fio', 'firstName'] as const, johnDoe); // => 'John' */

(pathToProp: P, obj: O): ValueByPath; /** * Retrieve the value at a given path. * * @param {[String]} paths The path to use. * @return {*} function to get data at `path` for a given object * @example * * const johnDoe = { * fio: { * firstName: 'John', * lastName: 'Doe', * }, * }; * const getLastName = path(['fio', 'lastName'] as const); * const lastName = getLastName(johnDoe); // => 'Doe' */

(pathToProp: P): (obj: O) => ValueByPath; } declare const _default: Path; export default _default;