如何访问父类中的枚举(打字稿)

时间:2014-09-19 19:42:18

标签: enums typescript webstorm

我们的应用程序基于类 而不是模块 (参考以下问题和答案:How do I declare a public enum in typescript?)。

以下是代码结构的示例:

// GrandParent.ts
enum DAYS {SUNDAY,MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY};
class GrandParent {
    // Some super cool stuff
} 
export = GrandParent

//Parent.ts
import GrandParent = require('./GrandParent');
class Parent extends GrandParent {
    // Some things even cooler
}
export = Parent

// Child.ts
import Parent = require('./Parent');
class Child extends Parent {
    getMonday():DAYS{           //Could not find symbol 'DAYS'
        return DAYS.MONDAY; 
    }
}
export = Child

当我尝试转换代码时,我在webstorm中收到以下错误:错误TS2095:找不到符号' DAYS'。

有没有办法以enum格式GrandParent访问Child

2 个答案:

答案 0 :(得分:1)

当您说export =时,它是从文件中导出事物的声明。

如果要从模块中导出多个内容,请使用多个export声明:

// GrandParent.ts
export enum DAYS {SUNDAY,MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY};
export class GrandParent {
    // Some super cool stuff
} 

//Parent.ts
import GrandParent = require('./GrandParent');
class Parent extends GrandParent.GrandParent {
    // Some things even cooler
}
var x = GrandParent.DAYS.MONDAY;
export = Parent

答案 1 :(得分:0)

这是我做的事情

module portal{

 enum DAYS {SUNDAY,MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY};


}


module portal.logic{

 //now in this module you can access enum of parent module no need for require

  export class GrandParent {
// Some super cool stuff
} 


 class Parent extends GrandParent.GrandParent {
// Some things even cooler
 }
}