Menu
CoddyTech

Typed Arrays

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

Typed arrays are one of Luau's most useful features for organizing multiple values while keeping type safety.

In Lua, an array is just a table with values at positions 1, 2, 3, …, and it will happily hold a number, a string and a boolean all at once. Luau lets you declare that a table should only contain elements of one type, using the {Type} syntax:

local numbers: {number} = {1, 2, 3, 4, 5}
local names: {string} = {"Alice", "Bob", "Charlie"}
local flags: {boolean} = {true, false, true}

When you declare a typed array, Luau's type checker enforces that only values of the declared type go in. Insert a string into a {number} array and you get a type error while editing, before the code ever runs:

local scores: {number} = {85, 90, 78}
table.insert(scores, "oops") -- ✗ type error: string is not a number

Everything you already know about Lua tables still applies: arrays are 1-based (scores[1] is the first element), #scores is the length, and table.insert / table.remove work as usual. The annotation adds safety, not new runtime behavior.

challenge icon

Challenge

Easy

Store scores and names in typed arrays.

  1. Declare local scores: {number} = {85, 92, 78}.
  2. Declare local studentNames: {string} = {"Alice", "Bob", "Charlie"}.
  3. Print scores[1], #scores and studentNames[2], one per line.

Press Run Code: the output shows 85, 3 and Bob.

Try it yourself

-- 1) declare scores: {number} and studentNames: {string}
-- 2) print the first score, the number of scores and the second name

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler