Menu
CoddyTech

Inline Shape Annotations

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 39 of 80.

In Lua you've been using tables as records for a long time: local user = {name = "Ana", age = 20}. Nothing stops you from misspelling a field, forgetting one, or storing a string where a number belongs. You only find out when the code runs.

Luau lets you describe the shape of such a table right in the declaration, listing each field and its type between curly braces:

local user: {name: string, age: number} = {
    name = "Ana",
    age = 20,
}

Compare this with the table types you already know: {number} describes an array and {[string]: number} describes a map with any string keys. A shape is more precise. It names each field individually and gives every field its own type.

Once the shape is declared, the type checker verifies the table against it while you edit. A missing field or a wrong value type is flagged before the code ever runs:

local user: {name: string, age: number} = {
    name = "Ana",
    age = "twenty", -- ✗ type error: string is not a number
}

As always, the annotation adds no runtime behavior: the table is a plain Lua table. Inline shapes are perfect for one-off structures you don't plan to reuse; in the next lesson you'll give shapes a name.

challenge icon

Challenge

Easy

Describe a student record right in its declaration.

  1. Declare local student: {name: string, studentId: number} = {name = "Sarah Johnson", studentId = 12345}.
  2. Print student.name, then student.studentId.

Press Run Code: the output shows Sarah Johnson and 12345.

Try it yourself

-- 1) declare student with an inline shape annotation
-- 2) print its name, then its studentId

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler