Machtyn
Machtyn

Reputation: 3272

How to use a static enum in another class in typescript

Using this question and answer as a reference, Can you set a static enum inside of a TypeScript class?, I created an enum and made it a static property in my class as follows

/* 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;
    }
}

Yes, this is a basic class, used as an example.

I have a second class in another file that needs to use it.

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;
    }
}

I'm using IntelliJ IDEA and have the "Language & Frameworks" -> TypeScript: "TypeScript version:" custom pointing to my current version, which is 2.0.2. IntelliJ is complaining to me and stating that it cannot find the namespace 'Input'.

Upvotes: 1

Views: 3243

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221192

If you want it to work this way, just write

/* 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 }
}

Upvotes: 4

Related Questions