typescriptdefinition

Enum inside class (TypeScript definition file)


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.

declare module 'Lib' {
  export module Graphics {
    export class Image {
      enum State {}

      static STATE_IDLE: State;
      static STATE_LOADING: State;
      static STATE_READY: State;
      static STATE_ERROR: State;
      constructor();
    }
  }
}

If I move State into the Graphics module it works but now State belongs to Graphics, which is incorrect. It needs to be part of Image.


Solution

  • I think I may have found a solution...whether it's valid TypeScript I don't know but it works and doesn't cause any compile errors. It's a combination of the above answers.

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

    Can anyone spot any potential issues with this that I haven't noticed?