3

I have declared the following enum in my Typescript file:

export const enum INPUT_PATTERNS{
    ALL          = ".*",
    ONLY_NUMBERS = "[0-9]*"

}

During compilation, I keep on getting the following error message:

In 'const' enum declarations member initializer must be constant expression.

I have initialized the enums with constant values and so I dont understand whats wrong here?

Secondly, if I remove the const identifier from enum as follows:

export enum INPUT_PATTERNS{
    ALL          = ".*",
    ONLY_NUMBERS = "[0-9]*"

}

then I get the following error:

Type '"."' is not assignable to type 'INPUT_PATTERNS'.
Type '"[0-9]
"' is not assignable to type 'INPUT_PATTERNS'.

1

2 Answers 2

1

You could do:

export enum INPUT_PATTERNS{
    ALL          = <any>".*",
    ONLY_NUMBERS = <any>"[0-9]*" 

}
Sign up to request clarification or add additional context in comments.

Comments

1

Enums allow us to define a set of named numeric constants. http://www.typescriptlang.org/docs/handbook/enums.html

You can use a combination of namespace and const variables:

export namespace INPUT_PATTERNS {
    export const ALL = ".*";
    export const ONLY_NUMBERS = "[0-9]*";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.