Enum inside class (TypeScript definition file)

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

    I think the following is an improvement on KoenT's solution:

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

    The advantage being that you can leverage named imports:

    import {Image} from './image';
    let img = new Image()
    img.state = Image.State.Error
    

提交回复
热议问题