I have the following interface definition:
interface foo1 {
add(num1: number, num2: number): number;
subtract(num1: number, num2: number): number;
}
Next, I instantiate an object in the shape of foo1:
let myFoo1: foo1;
myFoo1.add(5, 6); // OK
myFoo1.subtract(5, 6); // OK
So far so good. Now, I introduce a new interface that is meant to enhance the functionality of the first one:
interface foo2 {
add(num1: number, num2: number, num3: number): number;
}
Following this, I create a new object conforming to the foo2 structure:
let myFoo2: foo2;
myFoo2.add(5, 6, 7); // OK
myFoo2.subtract(5, 6); // ERROR
By basing myFoo2 on the foo2 interface, I gain access to an advanced version of the add method, but lose the ability to use the subtract function.
To address this issue, I came up with the following solution:
let myFoo2: foo1 & foo2;
myFoo2.add(5, 6, 7); // OK
myFoo2.subtract(5, 6); // OK
This approach allows me to utilize both the enhanced add method as well as the original subtract function.
Is this the correct way to handle such scenarios or am I missing something?