How do I tell TypeScript of additions to native types like Date,Number,String etc?
E.g. I want the following to compile (example from http://sugarjs.com/dates):
var date: Date = Date.create('tomorrow'); // I get a compile error
date.isAfter('March 1st') // I get a compile error
There are two kinds of extensions needed here.
e.g. create in the given example.
This can be done by adding to <type>Constructor interfaces. e.g.
interface DateConstructor {
create(query: string): Date;
}
e.g. isAfter in the given example.
This can be done by adding to <type> interface e.g.
interface Date {
isAfter(query: string): boolean;
}
interface DateConstructor {
create(query: string): Date;
}
interface Date {
isAfter(query: string): boolean;
}
var date: Date = Date.create('tomorrow'); // okay
date.isAfter('March 1st') // okay
This has been possible since TypeScript 1.4.
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