Currently, I am facing a challenge while trying to pass various combinations of arguments to a class constructor. The constructor has 2 optional arguments. Here is the code snippet:
class MyClass {
foo(a, b) {
return new MyClass(a, b);
}
bar(a) {
return new MyClass(a);
}
baz(b) {
return new MyClass(b);
}
constructor(a?: TypeA, b?: TybeB) {}
}
The first two instantiations are successful, however, the third attempt encounters an error: "Argument of TypeB is not assignable to parameter of TypeA". Baz(b) does work if I switch the order of parameters in the constructor to become constructor(b?: TybeB, a?: TybeA), but this results in foo(a, b) failing.
Is there an alternative approach to resolve this issue?