While available in TS, there's no readonly modifier in JS (at least in June, 2021). There's a long discussion about it, but unless I miss something, it didn't result in anything yet.
You can prevent rewriting the property with getter functions (shown in other answers), but it seems you're more interested in preventing accidental modification of the value object instead. If so, one option available now is using Object.freeze to emulate readonly:
class KeyHandler {
static KEYS = Object.freeze({
DIRECTOR: 'director',
VISION: 'vision',
ABOUT: 'about',
})
}
Quoting the docs:
A frozen object can no longer be changed; freezing an object prevents
new properties from being added to it, existing properties from being
removed, prevents changing the enumerability, configurability, or
writability of existing properties, and prevents the values of
existing properties from being changed. In addition, freezing an
object also prevents its prototype from being changed.
After that, any attempt to modify KeyHandler.KEYS innards will throw in strict mode. This doesn't block attempts to reassign this property though: this...
KeyHandler.KEYS = `Now I'm changed!`
... still works. To disallow this behaviour too, you'll have to combine:
const KEYS = Object.freeze({
DIRECTOR: 'director',
VISION: 'vision',
ABOUT: 'about',
});
class KeyHandler {
static get KEYS() { return KEYS }
}
Now attempt to rewrite the property will throw in strict mode, too:
TypeError: Cannot set property KEYS of class KeyHandler {
static get KEYS() { return KEYS }
} which has only a getter