Hi all! Imagine I have the following class:
class MyClass {
constructor(private prop?: string) {}
method(prop?: string) {
console.log(prop || this.prop);
}
}
I want to achieve the following:
If prop is passed to the constructor, then method() could be called without any parameters.
If class was constructed without prop parameter, then method() must be called with prop argument.
Something like this:
const instance1 = new MyClass('prop');
instance1.method(); // <-- could be called without arguments
const instance2 = new MyClass();
instance2.method('prop'); // <-- argument is required
Is it possible to achieve it with TypeScript?
Thank you!