Skip to content

Lens

Lens<S, A> = object

Defined in: Core/Lens.ts:24

Lens<S, A> focuses on a single value A inside a structure S, providing a composable way to read and immutably update nested data.

A Lens always succeeds: the focused value is guaranteed to exist. For optional or indexed focuses, use Optional<S, A>.

type Address = { city: string; zip: string };
type User = { name: string; address: Address };

const addressLens = Lens.prop<User>()("address");
const cityLens    = Lens.prop<Address>()("city");
const userCityLens = pipe(addressLens, Lens.andThen(cityLens));

pipe(user, Lens.get(userCityLens));             // "Berlin"
pipe(user, Lens.set(userCityLens)("Hamburg"));  // new User with city updated
pipe(user, Lens.modify(userCityLens)(c => c.toUpperCase())); // "BERLIN"

S

A

readonly get: (s) => A

Defined in: Core/Lens.ts:25

S

A


readonly set: (a) => (s) => S

Defined in: Core/Lens.ts:26

A

(s): S

S

S