Rec — record utilities
Plain JavaScript objects used as maps — Record<string, A> — are one of the most common data
structures in any TypeScript codebase. Rec is a small collection of utilities for working with
them in pipelines: data-last, curried, and returning Maybe wherever a key might not exist.
Safe lookup
Section titled “Safe lookup”Rec.lookup retrieves a value by key and returns Maybe to make the absence explicit:
This composes naturally with Maybe operations:
Rec.lookup always returns Maybe — if you need the raw undefined for interop with code that
expects it, plain obj[key] is still there.
Transforming values
Section titled “Transforming values”map is the most common operation — most record transformations are just “apply this function to
every value”. Reach for mapWithKey when the key matters in the transformation.
map transforms every value in a record, preserving keys:
mapWithKey receives both key and value:
Filtering
Section titled “Filtering”filter keeps entries where the predicate passes:
filterWithKey receives both key and value:
Picking and omitting keys
Section titled “Picking and omitting keys”pick returns a new record with only the specified keys:
omit returns a new record with the specified keys removed:
Both are type-safe: pick returns Pick<A, K> and omit returns Omit<A, K>, so the resulting
type reflects exactly which keys are present.
Merging
Section titled “Merging”merge combines two records. Keys in the second record take precedence over the first:
Keys, values, and entries
Section titled “Keys, values, and entries”fromEntries is the inverse — builds a record from key-value pairs:
entries and fromEntries pair well when you want to transform both keys and values by converting
to entries, mapping, and converting back:
Inspecting size
Section titled “Inspecting size”Combining with pipe
Section titled “Combining with pipe”Because all Rec functions are curried and data-last, they chain naturally:
Each step produces a new record — no mutation, no intermediate variables.
When to use Rec
Section titled “When to use Rec”Use Rec when:
- You’re transforming or filtering a
Record<string, A>and want the step to compose inpipe - You need
Maybe-returning key lookup instead ofundefined - You’re picking or omitting keys and want the resulting type to reflect exactly what’s present
Keep using plain object operations when:
- The operation is a one-liner in a function body where
obj[key]or spread is already clear - You don’t need the result to compose in a pipeline