如何告诉TypeScript添加本机类型,如Date,Number,String等?
例如,我想要编译以下内容(例如来自http://sugarjs.com/dates):
var date: Date = Date.create('tomorrow'); // I get a compile error
date.isAfter('March 1st') // I get a compile error
Run Code Online (Sandbox Code Playgroud)
这里需要两种扩展.
例如create,在给定的例子中.
这可以通过添加到<type>Constructor接口来完成.例如
interface DateConstructor {
create(query: string): Date;
}
Run Code Online (Sandbox Code Playgroud)
例如isAfter,在给定的例子中.
这可以通过添加到<type>界面来完成
interface Date {
isAfter(query: string): boolean;
}
Run Code Online (Sandbox Code Playgroud)
interface DateConstructor {
create(query: string): Date;
}
interface Date {
isAfter(query: string): boolean;
}
var date: Date = Date.create('tomorrow'); // okay
date.isAfter('March 1st') // okay
Run Code Online (Sandbox Code Playgroud)
自TypeScript以来,这是可能的1.4.
| 归档时间: |
|
| 查看次数: |
214 次 |
| 最近记录: |