Bhushan Chandak
Bhushan Chandak

Reputation: 13

TS2474 (TS) In 'const' enum declarations member initializer must be constant expression

Error code:

export declare const enum JSDocTagName {
    Desc = "desc",
    Id = "id",
    Meaning = "meaning",
}

Using Angular 6 with .net framework

Upvotes: 1

Views: 1302

Answers (1)

Fenton
Fenton

Reputation: 250942

Constant enums are erased during transpilation, so they leave no code behind them in the runtime app. All uses are substituted for the value throughout the app. Therefore, your declare keyword is redundant:

export const enum JSDocTagName {
    Desc = "desc",
    Id = "id",
    Meaning = "meaning"
}

Unlike normal enums, constant enums can't have certain kinds of calculated values, for example, this is allowed in normal enums, but not constant enums:

const x = 1;

enum A {
    Name = x,
    Age = x + 1
}

Your error would normally be because you are trying to do something like the above on a constant enum.

You can use some calculated values even in a constant enum - as long as the result is predictable, such as:

const enum A {
    Name = 1 << 0,
    Age = 1 << 1,
    Date = 1 << 2
}

Upvotes: 1

Related Questions