Reputation: 18445
I have a scenario where I have an interface which has a method like so:
interface SomeInterface
{
SomeMethod(arg1: string, arg2: string, arg3: boolean);
}
And a class like so:
class SomeImplementation implements SomeInterface
{
public SomeMethod(arg1: string, arg2: string, arg3: boolean = true){...}
}
Now the problem is I cannot seem to tell the interface that the 3rd option should be optional or have a default value, as if I try to tell the interface there is a default value I get the error:
TS2174: Default arguments are not allowed in an overload parameter.
If I omit the default from the interface and invokes it like so:
var myObject = new SomeImplementation();
myObject.SomeMethod("foo", "bar");
It complains that the parameters do not match any override. So is there a way to be able to have default values for parameters and inherit from an interface, I dont mind if the interface has to have the value as default too as it is always going to be an optional argument.
Upvotes: 50
Views: 41578
Reputation: 220964
You can define the parameter to be optional with ?
:
interface SomeInterface {
SomeMethod(arg1: string, arg2: string, arg3?: boolean);
}
Upvotes: 79