Codectionary / Developer documentation / Luau

Luau typeof-derived aliases

The typeof type expression derives a static type from an expression. It can keep a helper's return type tied to a prototype while leaving runtime type queries as a separate use of typeof.

Syntax

type Settings = typeof(defaults)

Examples

Luau typeof-derived aliases

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

--!strict
local defaults = {volume = 0.5, enabled = true}
type Settings = typeof(defaults)
local function muted(): Settings
    return {volume = 0, enabled = false}
end
local settings = muted()
print(settings.volume, settings.enabled)
  1. Output: 0 and false. Settings follows the shape of defaults; typeof in a type alias does not construct another settings object.

Best practices

  • Use aliases to share a stable public shape. Changing the prototype's inferred fields can change every dependent type.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading