In JavaScript, it is straight-forwardd to add functions and members to the prototype
of any type. I'm trying to achieve the same in TypeScript as follows:
interface Date
{
Minimum: Date;
}
Date.prototype.Minimum = function () { return (new Date()); }
This produces the following error:
Type '() => Date' is not assignable to type 'Date'.
Property 'toDateString' is missing in type '() => Date'.
Considering TS is strongly-types, how could we achieve this?
Since I'm writing a custom utility library in TS, I'd rather not resort to JS.
Interfaces don't get transpiled to JS, they're just there for defining types.
You could create a new interface that would inherit from the first:
interface IExtendedDate extends Date {
Minimum: () => Date;
}
But for the actual implementations you will need to define a class. For example:
class ExtendedDate implements IExtendedDate {
public Minimum(): Date {
return (new ExtendedDate());
}
}
However note that you can do all this without an interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With