Khebab-Case
Khebab-Case

Reputation: 21

How to prevent multiple 'as const' on TypeScript

How can I prevent multiple 'as const' needing by declaring an interface or a type to solve this issue below?

export const MESSAGES = {
    WELlCOME_MESSAGE: 'Wellcome again' as const,
    UNALLOWED_MESSAGE: 'You are not allowed' as const,
    ALLOWED_MESSAGE: 'You are allowed' as const,
}

Upvotes: 0

Views: 189

Answers (1)

Terry
Terry

Reputation: 66173

Just declare as const on the object level instead:

export const MESSAGES = {
    WELlCOME_MESSAGE: 'Wellcome again',
    UNALLOWED_MESSAGE: 'You are not allowed',
    ALLOWED_MESSAGE: 'You are allowed',
} as const;

See example on TypeScript Playground.

Upvotes: 6

Related Questions