Skip to content

Lens

Lens<S, A> = object

Defined in: Core/Lens.ts:31

Focuses on a required property or value A inside a parent structure S, providing a composable way to read, overwrite, and immutably update nested data.

A Lens assumes the target value is guaranteed to exist. For optional or indexed paths, use Optional<S, A>.

type Address = { city: string; zip: string };
type Customer = { id: string; address: Address };

const addressLens = Lens.from.property<Customer>()("address");
const cityLens = Lens.from.property<Address>()("city");
const customerCityLens = pipe(addressLens, Lens.andThen(cityLens));

const customer: Customer = {
  id: "cust_101",
  address: { city: "Berlin", zip: "10115" },
};

pipe(customer, Lens.get(customerCityLens)); // "Berlin"
pipe(customer, Lens.set(customerCityLens)("Hamburg"));
// { id: "cust_101", address: { city: "Hamburg", zip: "10115" } }
pipe(customer, Lens.modify(customerCityLens)((city) => city.toUpperCase()));
// { id: "cust_101", address: { city: "BERLIN", zip: "10115" } }

S

A

readonly get: (structure) => A

Defined in: Core/Lens.ts:31

S

A


readonly set: (value) => (structure) => S

Defined in: Core/Lens.ts:31

A

(structure) => S