Menu
CoddyTech

Type Assertions

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 74 of 80.

You already used :: once, in the enums chapter, to treat io.read() as a Direction. This lesson looks at it properly.

Sometimes you know more about a value than Luau's type checker does: a value typed any that you know is really a user record, for example. A type assertion (also called a cast) lets you tell the checker "trust me, treat this value as this type". Luau's cast operator is :::

type User = {id: number, username: string}

local raw: any = {id = 42, username = "alice_dev"}
local user = raw :: User
print(user.username) -- the checker now knows the fields

A cast changes only what the type checker believes. It does nothing at runtime. It never converts a value: "5" :: any is still the string "5", not the number 5 (use tonumber for real conversion). And it never validates: if you assert the wrong type, the cast itself won't error. Your code will simply misbehave later, when it touches fields that aren't there.

When to use it: when a value arrives as any (or a broad union) and you genuinely know its shape. When not to: to silence a type error you don't understand: the error is usually telling you about a real bug, and casting it away just hides it. Prefer real checks (like the typeof guards in the next lesson) when you can verify at runtime.

challenge icon

Challenge

Easy

Read a record that arrives typed any.

  1. Write type User = {id: number, username: string, isActive: boolean}.
  2. Write local function describeUser(data: any): string that casts with local user = data :: User and returns `User {user.id}: {user.username} (Active: {user.isActive})`.
  3. Print describeUser(rawUser2).

Press Run Code: the output shows User 15: bob_admin (Active: false).

Try it yourself

-- Raw records from outside the program: typed any
local rawUser1: any = {id = 7, username = "nina_dev", isActive = true}
local rawUser2: any = {id = 15, username = "bob_admin", isActive = false}

-- 1) type User = {...}
-- 2) describeUser(data: any): string, casting with ::
-- 3) print describeUser(rawUser2)

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler