2016-10-06 104 views
0

使用这个问题和答案作为参考,Can you set a static enum inside of a TypeScript class?,我创建了一个枚举,并使它在我的课的静态属性如下如何使用静态枚举另一个类的打字稿

/* Input.ts */ 
enum INPUT_TYPE { TEXT, RADIO, CHECKBOX } 
export class Input { 
    static INPUT_TYPE = INPUT_TYPE; 
    readonly inputType: INPUT_TYPE; 

    constructor (inputType: INPUT_TYPE) { 
     this.inputType = inputType; 
    } 
} 

是的,这是一个基本的类,用作示例。

我在另一个需要使用它的文件中有第二个类。

import {Input} from "./Input"; 
/* InputLabel.ts */ 
export class InputLabel extends Input { 
    readonly label: string; 

    constructor(label:string, inputType: Input.INPUT_TYPE) { 
     super(inputType); 
     this.label = label; 
    } 
} 

我使用的IntelliJ IDEA,并有“语言&框架” - >打字稿:“打字稿版本:”定制指着我目前的版本,这是2.0.2。 IntelliJ向我抱怨,并指出它无法找到命名空间'输入'。

+0

难道不应该从 “./Input”'{进口输入};'? –

+0

是的。我把它固定在上面。 – Machtyn

回答

2

如果你想要这样的工作方式,只写

/* Input.ts */ 
export class Input { 
    readonly inputType: Input.INPUT_TYPE; 

    constructor (inputType: Input.INPUT_TYPE) { 
     this.inputType = inputType; 
    } 
} 
export namespace Input { 
    export enum INPUT_TYPE { TEXT, RADIO, CHECKBOX } 
}