loading...
Cover image for Typescript : Creating Chainable Functions

Typescript : Creating Chainable Functions

jwp profile image John Peters ・1 min read

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();

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

Discussion

pic
Editor guide