I have a class with an overloaded method (two versions).
One version takes no arguments. The second can take two.
class DFD {
...
getEndDatetime(): string;
getEndDatetime(startTime?: string, duration?: number): string {
if (!startTime || !duration) {
return new Date(this.getEndDatetimePOSIX()).toLocaleString();
} else {
this.getEndDatetimePOSIX(startTime, duration);
return new Date(this.getEndDatetimePOSIX(startTime, duration)).toLocaleString();
}
}
...
}
When I call this.getEndDateTime("8/11/2019, 11:42:17 PM", 5)
, TypeScript gives me an error, "Expected 0 arguments, but got 2."
How do I satisfy TypeScript here?
I'm running Node v10.16.0, using TypeScript v3.5.2. I've tried switching the order of the overloads:
// Switch the order
...
getEndDatetime(startTime?: string, duration?: number): string;
getEndDatetime(): string {
...
}
...
TypeScript then highlights startTime
and duration
within the code, saying it can't find it.
I expected my first overload implementation to not throw any errors when called with two parameters, but it does.