JS Built-ins
TypeScriptbeginner

Interface β€” Readonly Array

πŸ“– Usage Example
interface Config { readonly options: ReadonlyArray<string> }
const c: Config = { options: ['a', 'b', 'c'] }
// c.options.push('d')  β†’ Error: read-only array

ReadonlyArray<T> prevents mutation methods like push and splice on array-type properties.

ReadonlyArray<T>

Challenge: Implement createReadonlyArray<T>(items) returning a frozen copy of the array.

const arr = createReadonlyArray([1, 2, 3])
Object.isFrozen(arr)  // β†’ true
πŸ’‘ Hints (1)
  • β€’ Spread the array first so the original is not frozen.