我想声明一个名为的类Date,它具有 Date 类型的属性(如 JavaScript Date 对象的 TypeScript 接口。但是编译器假定我的属性与我声明的类的类型相同。我怎么能区分两者?
如果 Date 接口在一个模块中,我可以使用模块名称来区分,但它似乎在一个全局命名空间中。我的Date班级在一个模块内。
我相信没有特殊的关键字来访问全局命名空间,但以下方法有效:
// Create alias (reference) to the global Date object
var OriginalDate = Date;
// Make copy of global Date interface
interface OriginalDate extends Date {}
module Foo {
export class Date {
public d: OriginalDate; // <-- use alias of interface here
constructor() {
this.d = new OriginalDate(2014, 1, 1); // <-- and reference to object here
}
}
}
var bar = new Foo.Date();
alert(bar.d.getFullYear().toString());
Run Code Online (Sandbox Code Playgroud)
另请参阅:https ://github.com/Microsoft/TypeScript/blob/master/src/lib/core.d.ts
过去,我总是将此类类命名为“DateTime”以避免此问题(以及可能的混淆)。
| 归档时间: |
|
| 查看次数: |
3442 次 |
| 最近记录: |