JS Built-ins
TypeScriptbeginner

Interface β€” Readonly Properties

πŸ“– Usage Example
interface Config { readonly host: string; readonly port: number }
const cfg = createFrozenConfig('localhost', 3000)
Object.isFrozen(cfg)   // β†’ true

readonly interface properties cannot be reassigned; Object.freeze enforces this at runtime.

Interfaces: readonly + Object.freeze

readonly properties can be set at creation but not mutated afterward. Object.freeze() enforces this at runtime.

Challenge: Implement createFrozenConfig(host, port) returning a frozen object.

const cfg = createFrozenConfig('localhost', 3000)
Object.isFrozen(cfg) // β†’ true
πŸ’‘ Hints (1)
  • β€’ Object.freeze prevents property mutation at runtime.