Enum inside class (TypeScript definition file)

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

    Here's my solution.

    program.ts:

    enum Status {
        Deleting,
        Editing,
        Existing,
        New
    }
    
    export class Program {
        static readonly Status = Status;
        readonly Status = Program.Status;
    
        title: string;
    
        status: Status;
    
        constructor(init?: Partial) {
            Object.assign(this, init);
        }
    }
    

    Usage:

    let program = new Program({ title: `some title` });
    
    program.status = Program.Status.New;
    

    or

    program.status = program.Status.New;
    

    Added benefit for Angular 2+ users: this can be used in templates

    Only display if status of the program is New

提交回复
热议问题