Home > database >  How to prevent multiple 'as const' on TypeScript
How to prevent multiple 'as const' on TypeScript

Time:02-09

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,
}

CodePudding user response:

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.

  •  Tags:  
  • Related