← Luau typing quick reference

Optional values

Luau 0.731 · --!strict
T? = T | nil

A question mark means that a value may be absent. Narrow away nil before using the value as T.

When to use it

Read this page when you need to apply Optional values, verify its exact contract, and adapt the example without bypassing validation or host boundaries.

Focused example

--!strict

type Color = "red" | "green" | "blue"
local selectedColor: Color? = "green"
local label = "No selection"

if selectedColor ~= nil then
    label = "Selected: " .. selectedColor
end

type Meditation = {
    id: string,
    description: string?,
}

local session: Meditation = { id = "morning" }
assert(label == "Selected: green" and session.description == nil)

Complete inventory

View all pages in Luau typing quick reference
← Previous: Enum-like singleton unionsNext: Unions and type narrowing →