Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: extending imported enum

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?

like image 999
smnbbrv Avatar asked Jan 30 '17 16:01

smnbbrv


People also ask

Can you extend an enum in TypeScript?

Can you extend enums? The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them.

Can enum be extended?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Should you use enums in TypeScript?

Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.

Should enums be capitalized TypeScript?

Use uppercase letters for your enums - they are global constants which have usable values. They are more than just types, which use usually like Foo or TFoo . The keys on an enum tend to be titlecase.


3 Answers

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! ;)

like image 169
Chklang Avatar answered Oct 10 '22 11:10

Chklang


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"

like image 42
VJPPaz Avatar answered Oct 10 '22 13:10

VJPPaz


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.

like image 36
smnbbrv Avatar answered Oct 10 '22 12:10

smnbbrv