Function PathLens

  • Creates a lens for a nested property.

    Returns

    A lens to the nested property.

    Example

    Given a data model:

    interface Company {
    readonly id: number;
    readonly name: string;
    readonly description?: string;
    readonly address: Address;
    }

    interface Address {
    readonly country: string;
    readonly region: string;
    readonly street: Street;
    readonly building: string;
    }

    interface Street {
    readonly name: string;
    readonly kind: string;
    }

    Create a lens to a nested property:

    const [
    getStreetName,
    setStreetName,
    overStreetName
    ] = PathLens<Company>()('address', 'street', 'name');

    Use "get" function to get a value of the nested property:

    console.log(getStreetName(company));           // "Broadway"
    

    Use "set" function to set a value of the nested property:

    const newCompany = setStreetName("Main")(company);

    console.log(newCompany.address.street.name); // "Main"

    Use "over" function to map and set a value of the nested property:

    const newCompany = overStreetName(toUpperCase)(company);

    console.log(newCompany.address.street.name); // "BROADWAY"

    Type Parameters

    • T extends object

      Type of the whole.

    Returns (<Path>(...path: Path) => Lens<T, DeepType<T, Path>>)

      • <Path>(...path: Path): Lens<T, DeepType<T, Path>>
      • Type Parameters

        • Path extends [keyof T] | [keyof T, ...DeepKeys<any[any]>[]]

        Parameters

        • Rest ...path: Path

        Returns Lens<T, DeepType<T, Path>>

Generated using TypeDoc