Enum inside class (TypeScript definition file)

后端 未结 7 1665
再見小時候
再見小時候 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:46

    I'm not sure what you intend to do, but I would have expected that you would want an enum to represent the possible state values, and then a state member on the image to indicate the current state of the image.

    declare module 'Lib' {
        export module Graphics {
    
            enum State {
                STATE_IDLE,
                STATE_LOADING,
                STATE_READY,
                STATE_ERROR
            }
    
            export class Image {
                public state: State;
    
                constructor();
            }
    
        }
    }
    

    It sounds like you want to declare a class that has enum-like members, rather than declare an enum within a class. i.e:

    declare module 'Lib' {
    
        export module Graphics {
    
            export class Image {
                static STATE_IDLE: number;
                static STATE_LOADING: number;
                static STATE_READY: number;
                static STATE_ERROR: number;
    
                constructor();
            }
        }
    }
    

提交回复
热议问题