Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与接口同名的TypeScript类_Typescript - Fatal编程技术网

与接口同名的TypeScript类

与接口同名的TypeScript类,typescript,Typescript,我想声明一个名为Date的类,该类的属性类型为Date(如JavaScript的TypeScript接口中)。但是编译器假定我的属性与我声明的类的类型相同。我如何区分这两个属性 如果日期接口位于模块中,我可以使用模块名称来区分,但是它似乎位于全局命名空间中。我的Date类位于模块中。我相信没有特殊关键字来访问全局命名空间,但以下方法可以工作: // Create alias (reference) to the global Date object var OriginalDate = Date

我想声明一个名为
Date
的类,该类的属性类型为Date(如JavaScript的TypeScript接口中)。但是编译器假定我的属性与我声明的类的类型相同。我如何区分这两个属性


如果日期接口位于模块中,我可以使用模块名称来区分,但是它似乎位于全局命名空间中。我的
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());
//创建全局日期对象的别名(引用)
var OriginalDate=日期;
//复制全局日期接口
接口原始日期扩展日期{}
模块Foo{
出口类别日期{

public d:OriginalDate;//你确定这样行吗?在我的TypeScript中,我无法像这样区分名称空间名称和我的Class.ps。链接不起作用