Menu
Coddy logo textTech

Inline Shape Annotations

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 37 of 73.

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

Create a variable named student with an inline shape annotation declaring:

  • name of type string
  • studentId of type number
  • isEnrolled of type boolean

Initialize it with name "Sarah Johnson", studentId 12345 and isEnrolled true.

Create a second variable named course with an inline shape annotation declaring:

  • title of type string
  • credits of type number
  • instructor of type string

Initialize it with title "Introduction to Luau", credits 3 and instructor "Dr. Smith".

Print, each on its own line:

  1. the student's name
  2. the student's ID
  3. the course title
  4. the number of credits

Try it yourself

-- Write code here
-- 1) declare student with an inline shape annotation
-- 2) declare course with an inline shape annotation
-- 3) print the four requested fields
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau

Practice on your own: Online Lua compiler