Shapes vs. Loose Tables
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 40 of 73.
You now have two ways to work with record tables: the loose way you've always used in Lua, and declared shapes. It's worth pausing to see what each buys you.
A loose table accepts anything. That flexibility has a price: misspell a field (user.nmae) and you silently get nil; forget a field and the bug surfaces far away, at runtime, in whatever code first touches the hole:
local user = {name = "Ana", age = 20}
print(user.nmae) -- loose table: silently prints nilDeclare a shape and those mistakes move from runtime to edit time. The checker flags the typo the moment you write it, your editor can autocomplete field names, and the type declaration doubles as documentation of what the table contains:
type User = {name: string, age: number}
local user: User = {name = "Ana", age = 20}
print(user.nmae) -- ✗ type error: key 'nmae' not found in UserIf you're coming from TypeScript, note that Luau has no interface keyword — type handles every shape. There's also no "declaration merging": declaring two types with the same name in the same scope is an error, so each shape has exactly one definition.
Luau's shapes are structural: any table with the right fields of the right types matches, no matter where or how it was created. And loose tables still have their place — quick scripts and truly dynamic data don't need a declared shape. For anything passed between functions, though, a shape catches mistakes while they're still cheap.
Try it yourself
This lesson doesn't include a code challenge.
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 Maps6Typing Table Shapes
Inline Shape AnnotationsType Aliases for ShapesOptional PropertiesShapes vs. Loose TablesExtending ShapesAdding Methods to ShapesSelf and Colon MethodsRecap: Defining Table Shapes