Functional Programming with TypeScript

Learn about the libraries that we can use to harness the power of functional programming with TypeScript.

We'll cover the following

Overview

We know that TypeScript adds types, but do we need anything else? Although JavaScript is one of the easier mainstream languages with which we can write elegant, functional code, it lacks several essential features. We’ll need some additional libraries or frameworks if we want to write real functional code, or the kind of stuff we can deploy to production.

Why use fp-ts?

We’ve already discussed Ramda Ramda and Folktale, but unfortunately, they’re insufficient. Ramda lacks the extremely useful concept of monads, on which several more advanced functional programming techniques are based. Folktale is a small library with some of the essential monads, but it has very few other features. It lacks some more powerful functional programming features like monad transformers. Another disadvantage of Folktale is that the library hasn’t received much support during the last few years.

Instead, we’ll use a TypeScript library called fp-ts. It has several advantages:

  • It’s written in TypeScript.
  • It has many stars on GitHub.
  • It’s actively maintained.
  • It has powerful monads.
  • It has an ecosystem of libraries that add even more functionality.

The author of the library and a few others have supported it with blog posts and discussed its functionality. Keep in mind that the documentation for fp-ts is OK but not great. In fact, the GitHub page carries a warning: “Teaching functional programming is out of scope of this project, so the documentation assumes you already know what functional programming is.”

Get hands-on with 1200+ tech skills courses.