In TypeScript, u can easily declare constants using as const
:
const Meal = {
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
}
If u are limited to JavaScript, here's 2 solutions:
- using
Object.freeze
const Meal = Object.freeze({
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
})
This introduces runtime restriction though.
- using JSDoc
/** @type {const} */
const Meal = /** @type {const} */ ({
Breakfast: 'breakfast',
Lunch: 'lunch',
Dinner: 'dinner'
})
see: https://stackoverflow.com/questions/64559624/jsdoc-equivalent-to-typescripts-as-const