DEV Community

Cover image for Typescript : Creating Chainable Functions
John Peters
John Peters

Posted on

Typescript : Creating Chainable Functions

A chainable function is one where we are able to do this:

   // These functions are chained
   someFunction()
      .getSomething()
      .setSomething()
      .saveSomething();
  // These function are not chained
  let result = someFunction();
  result.getSomething();
  result.setSomething();
  result.saveSomething();
Enter fullscreen mode Exit fullscreen mode

A very subtle difference in that chainable functions "hide" the object they are working with.

Typescript Intellisense

For a refresher on what Typescript is really about:

Typescript's intellisense uses its own language service to power autocompletion. Without diving too deep, if we as programmers adjust our thing to doing everything the Typescript way, we get, for free, classes and function which always work with Intellisense.
If we adopt the methods shown below we get the free Chainable option too.

Todo: Give examples

JWP2020 Chainable Functions in Typescript

Top comments (0)