Optional Properties
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 39 of 73.
Not every field is always present. In Lua, a missing table field simply reads as nil — which is flexible, but also how bugs like attempt to concatenate a nil value sneak in.
Luau lets a shape say this explicitly: add ? after a field's type to mark it optional:
type User = {
name: string, -- required
email: string?, -- optional: string or nil
}
local alice: User = {name = "Alice", email = "alice@luau.dev"}
local bob: User = {name = "Bob"} -- ✓ fine, email is optionalYou've met this before with variables: string? is shorthand for string | nil. The same idea now applies to individual fields of a shape.
Because an optional field might be nil, you must check before using it. The classic Lua pattern works, and in strict mode the checker narrows the type inside the branch:
if bob.email then
print(`email: {bob.email}`) -- here email is a string
else
print("no email on file")
endOptional means "may be absent" — it does not mean "any type". If email exists, it must still be a string.
Challenge
EasyCreate a type alias named User with:
nameof typestring(required)emailof typestring?(optional)
Create two users:
alice— name"Alice", email"alice@luau.dev"bob— name"Bob", no email
Create a function printContact that takes a User. If the user has an email it prints [name] - [email]; otherwise it prints [name] - no email.
Then:
- call
printContact(alice) - call
printContact(bob) - print the result of comparing
bob.email == nil
Try it yourself
-- Write code here
-- 1) define type User with an optional email field
-- 2) create alice (with email) and bob (without)
-- 3) write printContact with a nil check
-- 4) call it for both users, then print bob.email == nil
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