Search code examples
fp-ts

Using POJOs in fp-ts


I want to do what I think is the simplest thing, map through a plain old Javascript object, using fp-ts constructs.

There are a lot of operators I would like to use — map, filterMap, and so on — but they seem to require the Map type (which I assume is the Javascript Map object) — and there does not seem to be any easy way to convert back and forth between Map and Javascript object, which in Typescript is the standard representation of algebraic data types and of its Record<K,V> type.

This seems like a pretty big hole. Please don’t tell me I have to switch to lodash...


Solution

  • fp-ts/Record lets you work with Record<K, V> as if they were a Functor in V, so you can have

    import { map } from 'fp-ts/Record'
    
    const mapped = map((v: number) => v + 1)({a: 1}) // { a: 2 }