Typed Arrays
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 13 of 73.
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 numberEverything 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
EasyCreate a typed array named scores that can only hold numbers, initialized with 85, 92 and 78.
Create a second typed array named studentNames that can only hold strings, initialized with "Alice", "Bob" and "Charlie".
Then print, each on its own line:
- the first score
- how many scores there are (use
#) - the second student name
Try it yourself
-- Write code here
-- 1) declare scores: {number} and studentNames: {string}
-- 2) print first score, the scores count, and the second name
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps