← Luau typing quick reference

Unions and type narrowing

Luau 0.731 · --!strict
A | B

A value may match either type. A type, typeof, nil, or tag check narrows the union to a safe branch.

When to use it

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

Focused example

--!strict

local value: string | number = "hello"
local description: string

if type(value) == "string" then
    description = string.upper(value)
else
    description = tostring(value + 1)
end

assert(description == "HELLO")

Continue learning

Prerequisites

typeof

Complete inventory

View all pages in Luau typing quick reference
← Previous: Optional valuesNext: Tagged unions →