I'm interested in implementing function chaining in typescript.
Let's consider a sample class:
export class NumberOperator {
private num;
constructor(initialNum) {
this.num = initialNum;
}
public add(inc = 1) {
this.num += inc;
}
}
Using the class as (1):
let finalNumber = new NumberOperator(3);
console.log(finalNumber); // Output: 3
Using the class as (2):
let finalNumber = new NumberOperator(3).add();
console.log(finalNumber); // Output: 4
Using the class as (3):
let finalNumber = new NumberOperator(3).add().add();
console.log(finalNumber); // Output: 5
Using the class as (4):
let finalNumber = new NumberOperator(3).add().add(2).toString();
console.log(finalNumber); // Output: "6"
I would appreciate any guidance on achieving this. Thanks in advance :)