Runtime immutability with table.freeze
Luau 0.731 · --!stricttable.freeze(value)
Freeze a table so later writes fail at runtime. The operation is shallow: nested tables remain mutable unless they are frozen separately.
When to use it
Read this page when you need to apply Runtime immutability with table.freeze, verify its exact contract, and adapt the example without bypassing validation or host boundaries.
- table.freeze affects runtime mutation; it is separate from static typing.
- Freeze each nested table separately when deep immutability is required.
- Freezing a returned module API prevents consumers from replacing its exported fields.
Focused example
--!strict
type Tween = {
kind: "tween",
from: number,
to: number,
}
local tween = table.freeze({
kind = "tween",
from = 0,
to = 1,
}) :: Tween
-- tween.to = 2 -- runtime error: the table is frozen