Enum inside class (TypeScript definition file)

后端 未结 7 1698
再見小時候
再見小時候 2021-01-31 01:20

I\'ve searched around but can\'t seem to find an answer for this, hopefully you can help. How can I add an enum to Image? This is what I would like ideally but I get an error.

7条回答
  •  逝去的感伤
    2021-01-31 01:34

    I also bumped into this problem recently. This is what I am currently using as a solution:

    // File: Image.ts
    
    class Image
    {
        constructor()
        {
            this.state = Image.State.Idle;
        }
    
        state: Image.State;
    }
    
    module Image
    {
        export enum State
        {
            Idle,
            Loading,
            Ready,
            Error
        }
    }
    
    export = Image;
    

    Then in the place where I'm using the class and its enum:

    import Image = require("Image");
    
    let state = Image.State.Idle;
    let image = new Image();
    state = image.state;
    

    This seems to work fine (even though I don't consider it as the expected way to do this kind of thing).

    Hopefully there will be a way in TypeScript to do it this way:

    class Image
    {
        enum State
        {
            Idle,
            Loading,
            Ready,
            Error
        }
    
        constructor()
        {
            this.state = State.Idle;
        }
    
        state: State;
    }
    
    export = Image;
    

提交回复
热议问题