21

I can merge enum declarations within a single file e.g.

export enum Test {
  value1 = <any>'value1',
  value2 = <any>'value2'
}

export enum Test {
  value3 = <any>'value3'
}

This works fine, but my intention is to have a shared enum which I can extend later, e.g.

// test.enum.ts
export enum Test {
  value1 = <any>'value1',
  value2 = <any>'value2'
}

// place-to-extend-enum.ts
import { Test } from './test.enum';

export enum Test {
  value3 = <any>'value3'
}

What I get is

Individual declarations in merged declaration 'Test' must be all exported or all local.

Is there a way to achieve the desired behaviour?

smnbbrv
  • 23,502
  • 9
  • 78
  • 109

3 Answers3

21

In refer to https://github.com/Microsoft/TypeScript/pull/6213 you can do :

// test.enum.ts
export enum Test {
  value1 = <any>'value1',
  value2 = <any>'value2'
}

// place-to-extend-enum.ts
import { Test } from './test.enum';

declare module './test.enum' {
  export enum Test {
    value3 = <any>'value3'
  }
}

... Magic! ;)

Chklang
  • 857
  • 1
  • 8
  • 17
  • This gives me `Error:(25, 14) TS1066:In ambient enum declarations member initializer must be constant expression.`, so it does not really work with the strings. But I must admit, if I don't assign `value3` it does work. +1 though – smnbbrv Jul 10 '17 at 12:12
  • It works with typescript 2.4 which support natively enum as string – Chklang Jul 10 '17 at 12:47
  • ok, managed to make it work, only v2.4 and still it does not create a new enum. However, it mutates the existing one, so in other words it is still not possible to have another enum... Apart from that, the answer looks nice to me. Thanks! – smnbbrv Jul 11 '17 at 08:39
  • 8
    This doesn't seem to actually work. It extends the interface of the enum so that `value3` is considered a valid compile-time value, but the actual runtime value is still undefined. – Jeff Mercado Jun 11 '20 at 00:14
9

I saw a way that you can add additional function/method in an existing enum. this is by create the function within a namespace similar to the enum type: Here

enum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
namespace Weekday {
    export function isBusinessDay(day: Weekday) {
        switch (day) {
            case Weekday.Saturday:
            case Weekday.Sunday:
                return false;
            default:
                return true;
        }
    }
}

const mon = Weekday.Monday;
const sun = Weekday.Sunday;
console.log(Weekday.isBusinessDay(mon)); // true
console.log(Weekday.isBusinessDay(sun)); // false

You can see the complete information here https://basarat.gitbooks.io/typescript/docs/enums.html at section "Enum with static functions"

VJPPaz
  • 917
  • 7
  • 21
3

After some research I must admit I cannot find a super-proper way to do that.

But there are two possible solutions that are not that bad and not stinking that much.

First is implementing a custom enum - this way is not allowing to consume already existing enums. This is probably the only limitation of this method. Other than that it looks simple and quite native.

Another way is a big hackaround with [merging enums into one value with a separate type declaration. This way allows to consume already existing, real enums; however it is less comfortable to use because there are two entities to be aware of: enum value and enum type.

Graham
  • 7,431
  • 18
  • 59
  • 84
smnbbrv
  • 23,502
  • 9
  • 78
  • 109