To ensure a constant value, I would implement two key steps:
Firstly - guarantee that the property remains unchangeable at runtime.
///config.js
const config = {
anotherProp: true,
ecma: 2017
};
Object.defineProperty(config, "ecma", { value: 2017, writable: false });
export default config;
Secondly, create a d.ts file to validate in TypeScript that this property is read-only
/// config.d.ts
type Config = {
anotherProp: string;
readonly ecma: 2017;
};
declare const conf: Config;
export default conf;
This approach will prompt TypeScript to flag any attempted modifications
//ts error: Cannot assign to 'ecma' because it is a read-only property.ts(2540)
config.ecma = 6;
Additionally, any attempts to modify it through hacking will result in runtime errors:
//runtime error: Cannot assign to read only property 'ecma' of object '#<Object>'
(config as any).ecma = 6;
Access the playground link for further exploration: here