Codectionary / Developer documentation / Luau

Luau intersection record types

An intersection requires a value to satisfy both component types. It can combine compatible record capabilities without copying or merging tables at runtime.

Syntax

type Entity = Named & Positioned

Examples

Luau intersection record types

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
type Named = {name: string}
type Positioned = {x: number, y: number}
type Entity = Named & Positioned
local function describe(entity: Entity): string
    return entity.name .. "@" .. tostring(entity.x) .. "," .. tostring(entity.y)
end
print(describe({name = "spawn", x = 2, y = 5}))
  1. Output: spawn@2,5. The supplied record satisfies both the name and coordinate requirements.

Best practices

  • Avoid contradictory property types in intersections. Use a union when a value may have either shape rather than both.

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