Syntax
--!strict
type Result = { value: number, valid: boolean }Examples
Describe a function boundary
Standalone Luau or a Script in ServerScriptService. Script Analysis rejects a string passed to double. The annotation does not convert "6" into 6.
--!strict
local function double(value: number): number
return value * 2
end
local result = double(6)
print(result) -- 12Refine an unknown value
A runtime type check narrows unknown before arithmetic. Try passing a string and then 5; the first returns nil and the second returns 10.
--!strict
local function doubleIfNumber(value: unknown): number?
if type(value) ~= "number" then
return nil
end
return value * 2
end
print(doubleIfNumber("5"))
print(doubleIfNumber(5))Best practices
- Add --!strict at the start of a file and fix the warnings it exposes rather than silencing them with any.
- Use unknown for untrusted values until a runtime check establishes their shape.
- A type assertion (::) changes what the checker believes; it does not sanitize data or change its runtime value.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation