DEV Community

Discussion on: Converting Lodash to fp-ts

Collapse
cdimitroulas profile image
Christos Dimitroulas

Here's an example I used in a PR at work the other day to advocate against using lodash now that we've moved to Typescript:

import * as A from "fp-ts/lib/ReadonlyArray";
import { flow } from "fp-ts/lib/function";
import fp from "lodash/fp";
// This compiles no problem! Somehow lodash is happy to compose a function
// which returns a number with a function which accepts `null | { name: string }`
// myFn is typed as `(...args: any[]) => any` as well, which can cause other
// problems later down the line
const myFn = fp.pipe(
  (x: number[]) => x.reduce((accum, val) => accum + val, 0),
  (banana: null | { name: string }) =>
    "haha I can compose random functions together" + JSON.stringify(banana),
);
// This errors with `Type 'number' is not assignable to type '{ name: string; } | null'`
const myFn1 = flow(
  (x: number[]) => x.reduce((accum, val) => accum + val, 0),
  (banana: null | { name: string }) =>
    "haha I can compose random functions together" + JSON.stringify(banana),
);
Enter fullscreen mode Exit fullscreen mode