Method chaining in TypeScript

Contai, West Bengal, India
Search for a command to run...

Contai, West Bengal, India
No comments yet. Be the first to comment.
How I generate per-page Open Graph images at build time using satori and resvg-js - no edge runtime, no external service, just a custom Astro integration.

Ever wondered how you can keep your Telegram groups or online communities safe from inappropriate content automatically? It's actually easier than you might think! In this article, I'll walk you throu

Modern web and mobile applications typically use JWT (JSON Web Tokens) for authentication. These tokens expire after a certain period for security reasons, requiring a refresh mechanism to maintain us

When developing forms in React Native applications, we frequently encounter these challenges: Repetitive Code: Setting up input fields with proper styling and behavior for each form Validation Compl

Method chaining is a very useful and popular method in JavaScript and TypeScript which helps you write more readable and concise code. In this article, I will explain how method chaining works in JS/TS and how can you write your chainable methods.
a method is a function that is associated with an object or a class. Methods are used to encapsulate functionality that is related to a particular object or class and to provide a way to access and manipulate the data that the object or class contains.
Methods are declared within an object literal or class definition, and they are called on an instance of the object or class. For example
class Person {
hobbies: Array<string> = [];
// A method that adds a hobby
addHobby(hobby: string) {
this.hobbies.push(hobby);
}
}
Let's have a look at a popular example that uses method chaining :
import express from "express";
const app = express();
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
});
In the above example, you can see app is an express instance and it has four functions/methods chained, route, get, post & put. Looks cool right?
If we breakdown the express.js example it works the same as the below code
import express from "express";
const app = express();
app
.route('/book')
.get((req, res) => {
res.send('Get a random book')
});
app
.route('/book')
.post((req, res) => {
res.send('Add a book')
});
app
.route('/book')
.put((req, res) => {
res.send('Update the book')
});
so how does this work? the answer is pretty simple, each of the methods here returns the instance itself, thus you can continue adding new methods after using each of them, or chain the methods.
Let's create one class with a few methods, A Calculator class for example -
// Example without chaining methods
class Calculator {
private n: number;
constructor(num = 0) {
this.n = num;
}
add(num: number) {
this.n += num;
return this.n;
}
sub(num: number) {
this.n -= num;
return this.n;
}
multiply(num: number) {
this.n *= num;
return this.n;
}
divide(num: number) {
this.n /= num;
return this.n;
}
get answer() {
return this.n;
}
}
// Usage
const calc = new Calculator(10);
calc.multiply(10);
calc.add(2);
calc.divide(2);
console.log(calc.answer); // Outputs 51
Now, let's make those methods chainable by returning this or the instance itself, so that it can handle chaining methods.
// Example with chaining methods
class Calculator {
private n: number;
constructor(num = 0) {
this.n = num;
}
add(num: number) {
this.n += num;
return this;
}
sub(num: number) {
this.n -= num;
return this;
}
multiply(num: number) {
this.n *= num;
return this;
}
divide(num: number) {
this.n /= num;
return this;
}
get answer() {
return this.n;
}
}
// Usage
const calc = new Calculator(10);
calc.multiply(10).add(2).divide(2);
console.log(calc.answer); // Outputs 51
As you can see, now the methods are chainable and it looks much more concise.
I hope you learned something new today, If you think I have made some mistakes please do let me know in the comments. Thanks, and see you again in my next post.