DEV Community

Acid Coder
Acid Coder

Posted on

5 2

Typescript API Design: Single Callable Or Multiple Callable

Let's start with an example, let's say the requirement is to create a 'travel' API where it takes 'destination' and 'transportation' as arguments.

Currying is the process of transforming a function into multiple callable, but here I refer to it as a function that returns another function based on the number of parameters.

How would you design the API, with Typescript, which pattern you use?

A. plain simple function

const travel = (destination, transportation) => {
  // do something
}

Enter fullscreen mode Exit fullscreen mode

B. curry form

const travel = (destination) => (transportation) => {
  // do something
}
Enter fullscreen mode Exit fullscreen mode

C. method chaining

const travel = (destination) => {
   return {
       transport: (transportation)=>{
             // do something
       }
   }
}
Enter fullscreen mode Exit fullscreen mode

currying and method chaining are similar, because it involves multiple callable, while plain function is only one callable.

I prefer single callable, for a very simple reason, less prone to error when you call it.

Here is how you call them, keep in mind we are using Typescript

A. plain function

travel('North Pole', 'swimming') // ok
travel('North Pole') // ts error
Enter fullscreen mode Exit fullscreen mode

B. Currying

travel('North Pole')('swimming') // ok
travel('North Pole')() // ts error
travel('North Pole') // ok, but do nothing, no ts error
Enter fullscreen mode Exit fullscreen mode

C. Method chaining

travel('North Pole').transport('swimming') // ok
travel('North Pole').transport() // ts error
travel('North Pole') // ok, but do nothing, no ts error
Enter fullscreen mode Exit fullscreen mode

As you can see, it is impossible to call the plain function incorrectly with Typescript.

However this is not the same with currying and method chaining where it is possible to call them partially if you don't pay attention

Still currying and method chaining are good for reusing arguments

const goToNorthPoleWith= travel('North Pole')
goToNorthPoleWith('Airplane')
goToNorthPoleWith('Ship')
Enter fullscreen mode Exit fullscreen mode

so to have the best of both worlds, we should always start from single callable, and if we want to reuse the argument, we can curry it afterwards.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
aarone4 profile image
Aaron Reese

If the internal logic of the function is different based on the arguments passed in I would argue for method chaining so that the result of the first function becomes the argument for the second function. Each function can then be tested independently

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay