DEV Community

Discussion on: Converting Lodash to fp-ts

Collapse
anthonyjoeseph profile image
Anthony G

Great article! Here are another couple simple examples showing the advantages of fp-ts's strong type paradigms:

import sortBy from 'lodash/sortBy'
import { flow, map, takeRight } from 'lodash/fp'
import { pipe } from 'fp-ts/function'
import * as A from 'fp-ts/ReadonlyArray'
import * as Ord from 'fp-ts/Ord'
import * as N from 'fp-ts/number'

interface Result { rank: number; value: string }
declare const results: Result[]

const lodashSort = sortBy(results, 'ranck') // <-- no type error
const fptsSort = pipe(
  results,
  A.sort(pipe(
    N.Ord,
    Ord.contramap((b: Result) => b.ranck) // <-- type error
  ))
)

const lodashFlow = flow(
  map((r) => r.rank + 3), // <-- type error: Object is of type 'unknown'
  takeRight(2),
)(results)
const fptsPipe = pipe(
  results,
  A.map((r) => r.rank + 3), // <-- no type error
  A.takeRight(2)
)
Enter fullscreen mode Exit fullscreen mode