Quick Thoughts on Effect-TS
Tags: javascript
Published: 2023-07-11 11:39:01


---
title: Quick Thoughts on Effect-TS 
published: true
description: 
tags: JavaScript
canonical_url: https://jessewarden.com/2023/07/quick-thoughts-on-effect-ts.html
cover_image: 
---

Reading the [effect-ts docs](https://effect.website/), and it’s basically a Result and Option with many helper functions, a way to compose functions, with type-safety (as much as you can get in TypeScript). If you’ve ever used [Folktale](https://folktale.origamitower.com/), [Sanctuary](https://sanctuary.js.org/), or [true-myth](https://github.com/true-myth/true-myth), you’ll immediately know the basics. The run API reminds me of Python’s [async io](https://docs.python.org/3/library/asyncio.html) or [Scala’s ZIO](https://zio.dev/).

Unlike the other libraries, though, you get TypeScript type safety, with training wheels to learn how to stop using exceptions (e.g. throw) and embrace returning errors as values. Like any good ML style, they differentiate between the different type of I/O with types so you know “this side-effect, console.log and some other stuff, will never fail” (safe) vs. “I’m going to fetch something… then parse the response…” (unsafe).

Like [RxJS](https://rxjs.dev/), or [Bluebird](http://bluebirdjs.com/docs/getting-started.html), it has all the helper functions to integrate with sync, async, callbacks, and to transform between.

Sadly, because of the type safety, interfaces, and the various support for running different types of effect workflows, like RxJS, they have to create their \_own\_ [pipelines](https://www.effect.website/docs/essentials/pipeline). Given the challenges of JavaScript promise API’s, and the alpha state of the [JavaScript pipeline proposal](https://github.com/tc39/proposal-pipeline-operator), they provide good function composition API’s you’d expect from a library giving you typed functional programming. The only downside for beginners is that you’ll have to box and unbox things for yourself; it’s not all flatMapped, which makes sense from a type-safety perspective, but beginners may want to brush up on JavaScript Promises first, then Array concat and flat + flatMap as a warm up.

Unlike a long JavaScript promise chain, instead of re-running the code over and over, commenting out various sections with logs/taps, you have this thing called a “TypeScript compiler” to help you identify what’s coming in and what should go out. That’s what a lot us Functional Programmers were missing, hence using tools like [ReScript](https://rescript-lang.org/), [Elm](https://elm-lang.org/), and true-myth. Some utilize [JSDocs](https://jsdoc.app/), but… they still talk openly to Midna.

Speaking of tap, yes, it comes with a typesafe [tap function](https://www.effect.website/docs/essentials/pipeline#tap).

I should point out here that by default, all errors short circuit the pipe, just like a Promise chain does, so you’ll feel right at home. That, and they’re lazy by default; although, I’m too new to know if they’re not eager by default like JavaScript Promises.

Effect is also another brave enough to throw their hat into the ring, claiming they can possibility resurrect [generators](https://www.effect.website/docs/essentials/using-generators) in JavaScript, and have people \_desire\_ to use them vs. “oh god, not the star functions again…”. They clothed it to look like async/await. While I applaud their effort, if you’re using async/await syntax, you probably aren’t too keen to change your imperative style to functional style, and thus probably not going to use it with Effect-ts. But who knows, maybe someone will use that to write an imperative Seq (e.g. infinite array, called Sequence, [F#’s is pretty nice](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/sequences)).

Their use of \_ symbol for holes will immediately be familiar, but their use of it as a function parameter that you \_actually\_ use later will not. Be wary. Cool convention, it just may throw you off if you’re coming from FP languages.

If you’re on drugs, check out [Effect.loop](https://www.effect.website/docs/essentials/control-flow#loop-operators). Someone smart asked “How can we make a for loop deterministic and palatable to Functional Programmers?” they then smoked crack and did a good job.

There are hints of pattern matching throughout. It’s not true pattern matching like the [JavaScript proposal](https://github.com/tc39/proposal-pattern-matching), or what you’re used to in languages like [ReScript](https://rescript-lang.org/docs/manual/latest/pattern-matching-destructuring) and [Rust](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html), but it \_is\_ there, like in [Effect.exit](https://www.effect.website/docs/essentials/exit); FORCING developers to handle the un-happy path. A few of their helper types include a match function which users of Folktale should feel right at home.

Given this is TypeScript, and thus JavaScript, it’s impossible to trap all errors. Despite not giving you types for it, Effect-TS \_still\_ captures them, and that’s a pretty dope safety feature that any FP developer will love.

Sadly, like everything in TypeScript, making simple types requires a lot of syntax, and sometimes you have to define things twice. Such is the case in custom errors in their docs. They’ve followed the standard of making a class in JavaScript, which is common for those creating classes that extend Error to pattern match between the different error types. I’d prefer a simple [TypeScript Discriminated Union](https://jessewarden.com/2022/12/typescript-enums-vs-discriminated-unions.html) (which is verbose compared to other functional languages unions, but… it’s still a Union). However, I respect their decision to use interface instead of type, and class instead of function/object to extend an olive branch to the curious OOP devs. Being friendly is good. Still, there \_are\_ reasons they’re using \_tag symbols as you’ll read later in the docs, and having this metadata is helpful to provide helpful compiler errors as well as various filters that run behind the scenes throughout pipelines (e.g. [catchTags](https://www.effect.website/docs/tutorial/error-management/expected-errors#catchtag)).

To be clear, it doesn’t have to be “\_tag”; TypeScript just needs a differentiator; something unique for each of your interface or types, so the compiler can tell the difference vs. using the 1990’s style of “[instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof)” which isn’t type safe.

Speaking of unions, further down, they \_do\_ show if you’re doing an operation where multiple occurs could occur, you can use a simple union type in the type def for whatever comes out of your effect chain which \_is\_ more normal and extremely helpful.

Rather then put all the onus on the programmer to create “as safe code as you can possibly create in TypeScript”, they provide a [catchAll](https://www.effect.website/docs/tutorial/error-management/expected-errors#catchall) method, thank god.

They also have cancelable Promises called “[Fibers](https://www.effect.website/docs/tutorial/interruption-model#what-is-a-fiber)“. If you’re familiar with [Folktale’s Tasks](https://folktale.origamitower.com/api/v2.0.0/en/folktale.concurrency.task.html), they’ll look familiar, although the engineering behind it is more than just “a Promise I can stop and clean up resources afterwards”. They’ve also chosen the immediate abort within reason which will make you not freak out about possible mutable state. They handle all the concurrency bits too if you’re doing many sequential things (e.g. Promise.then.then.then ) as well as parallel (e.g. Promise.all).

The [Scope](https://www.effect.website/docs/tutorial/resource-management/scope) is nice in that it provides a common way to handle state-full resources. In Folktale Tasks, when your Promise was completed, whether it succeeded or failed, you could run a cleanup function. This is useful for things like closing database connections, saving files, flushing log results, moving iterators forward, etc. Effect-TS has incorporated BOTH types, not just releasing the resource, but getting it as well. For example, 90% of us programmers do NOT care if closing a Postgres connection actually works, especially if you’re in AWS Lambda for example. But connecting? Absolutely. These sometimes get complex too because they’re error prone, change per environment, etc. I’m not sure I dig the implementation yet, but I love the idea.

For those of us building libraries, they provide the basic [logging](https://www.effect.website/docs/tutorial/observability/logging) details between all the pipes you’d expect so you can [forward to a logger](https://www.effect.website/docs/guide/observability/custom-logger), like Winston/Pino/Bunyan that is the forwarded to CloudWatch/ElasticSearch/Splunk, etc.

I find it interesting that Effect-TS supports both [data-first and data-last](https://www.javierchavarri.com/data-first-and-data-last-a-comparison/). It’s data-last like I’d expect for anything FP trying to be friends with a JavaScript developer. However, many of their API’s support data-first, AND data-last… which they call “dual”. While it looks like partial application, they just call it “overload”. I have a feeling most people will use data-last to be more explicit, despite TypeScript helping you get the curried function right, but whatever.

For some reason, they author(s) weren’t done making new friends with this library, so they also made it their mission to help [stop primitive obsession](https://jessewarden.com/2022/02/avoiding-primitive-obsession-in-6-%c2%bd-programming-languages.html) as well. You can’t natively create distinct nominal types in TypeScript like you can in Elm or ReScript without making an unreadable mess that no one will write, and then just revert to using primitives everywhere. So Effect-TS created [Branded types](https://www.effect.website/docs/guide/style/brands). Now 2 strings passed to a function will always be in the right order. Simple stuff, nice add-on feature, but not really related to effects, but SUPER important for type-safety. Not complaining, just interesting to see this “additional unrelated helper that’s awesome”.

Not going to say much on their [do notation](https://www.effect.website/docs/guide/style/do) beyond herculean effort, well done giving your language constraints.

I was impressed to see a [Jitter](https://www.effect.website/docs/guide/scheduling/schedule-combinators#jittering) in the scheduling portion. For those of us UI developers who endeavor NOT to create [retry-storms](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/retry-storm/) and accidental DDoS attacks when dealing with flaky API’s we’re forced to call, it’s nice to have that safety feature just built-into the library. Useful on the API side as well, sure, you just rarely see it on the UI side.

It’s not all sunshine and FP rainbows, though… this is JavaScript. So Effect-TS provides a [Ref](https://www.effect.website/docs/guide/state-management/global-shared-state) to update mutable state, and share it between various Fibers. If mutable state is your thing, it can be super helpful. Sadly it’s against [my religion](https://en.wikipedia.org/wiki/Immutable_object).

Their [batching and concurrency controls](https://www.effect.website/docs/guide/batching-caching) are quite nice, but I get Angular vibes, and not good ones, with the amount of code and dependencies required just to setup an HTTP call. Having it type-safe with clear reasons why something failed is WORTH writing all that code for, no doubt, but you can get it with a lot less in ReScript or Elm, they just don’t have any of the concurrency/batching batteries included.

Obviously, Jesse Warden is going to be super irate when I see this young project having such wonderful documentation, but the “[Testing](https://www.effect.website/docs/guide/testing)” part is “TODO”. In my 20’s I’d be fine with it, it’s clear quality is baked in, but while I haven’t verified they have a test suite, I’m pretty strict nowadays about a testing culture, and so it’s super disappointing this part isn’t front and center.

It’s clear there’s a Scala influence because of the heavy FP/Zio vibes, which are great, and then the free use of the class keyword. FP zealots like myself ban that from their religion, but Scala devs have spent over a decade working with JVM code, so class is “a fine and accepted data type” for them. For use non-Scala FP’ers, class is NOT a datatype (DTO’s don’t count). So I’ve tried to keep that in mind when reading their docs that 99% of it could change interface to type, and class to {}.

If you’re an FP coder forced in TypeScript, Effect-TS is worth a look. If you’re new to Functional Programming, Effect-TS gives you all the basic primitives you need which are:

- a way to stop using Exceptions
- a way to model safely the absence of things (null/undefined)
- a way to build programs by composing functions together
- and do it in a type-safe way vs. un-typed JavaScript Promises

There’s lots more to come based on their docs and repo.

<a href=""></a><https://effect.website/>