TypeLens
TypeLens is a typesafe lensing library for JavaScript and TypeScript.
Introduction
TypeLens is inspired by and borrows ideas from Ramda and Fantasy Land. It is made to safely access properties in unknown objects.
Usage
import { lensKey } from 'typelens';
// Fetch some unsafe data.
const data = await fetch(url).then(r => t.json());
// Define a lens for a nested property.
const nameLens = lensKey(['user', 'profile', 'name']);
// Read the value at the focused property using `view`.
// Accessing a value with a lens returns a `Maybe` type.
const nameMaybe = lensKey.view(data);
// Safely resolve the value to a string. If the value is
// undefined, 'anonymous' used used as fallback.
const name: string = nameMaybe.getString('anonymous');