As Const and Object.freeze

What

as const is a TypeScript assertion that makes an entire object and all its nested properties readonly. Object.freeze() is a JavaScript method that prevents an object from being modified at runtime.

How

as const affects the TypeScript type system, while Object.freeze() affects the actual JavaScript execution:

// Recursive compile-time immutability
const config = {
  api: "https://api.com",
  ports: [80, 443]
} as const;

// Runtime immutability
const frozen = Object.freeze({ name: "Admin" });

Why

as const is essential for creating true constants, as it also narrows string values to their literal types. Object.freeze() provides an extra layer of security by ensuring that even if someone bypasses the TypeScript compiler, the object cannot be mutated while the program is running.

Summary

Combine as const and Object.freeze() for mission-critical constants that must remain immutable across all layers of your stack.