I'm currently facing an issue while implementing a set of chained functions.
interface IAdvancedCalculator {
add(value: number): this;
subtract(value: number): this;
divideBy(value: number): this;
multiplyBy(value: number): this;
calculate(): void
}
interface ISpecialAdvancedCalculator extends IAdvancedCalculator {
specialAdd(value: number): IAdvancedCalculator;
specialSubtract(value: number): IAdvancedCalculator;
}
let myCalculator: ISpecialAdvancedCalculator;
myCalculator
.add(20)
.multiplyBy(2)
.specialAdd(40)
.subtract(5)
.specialSubtract(20) //<-- Error! Property 'specialSubtract' does not exist on type 'IAdvancedCalculator'.
.calculate()
I am aiming to ensure type checking for the functions in the chain. Specifically, I want specialAdd
and specialSubtract
functions defined in ISpecialAdvancedCalculator
to be used only once each, while IAdvancedCalculator
functions can be used multiple times. As a TypeScript beginner, I have tried various approaches like Advanced types (Pick
& Omit
) without any success. Are there any other solutions I can explore to address this scenario?