Lesson 6 of 6

Types: strict mode, the new type solver, generics

Learn Luau's type system for Roblox. Strict mode, annotations, singleton unions, unknown and refinement, typeof, generics, type packs, the new type solver's general release and luau-lsp analyze in CI, with Beacon Yard's Types.luau.

Tool lesson, about 75 minutes, 12 steps

By the end you can

  • Read and write type annotations, aliases and singleton unions such as Beacon Yard's RoundPhase
  • Narrow an unknown value from outside into a known type, and say why the narrowing has to run at run time
  • Write a generic function and read a type pack at an intro level
  • Run the type checker outside Studio with luau-lsp analyze, and say where the new type solver stands

Before this: Objects in Luau: metatables, classes, composition

Step 1 of 12

Do not trust values from outside

When the server and a client send messages to each other (which Module 3 covers), the receiving side cannot trust what arrived. Later in Beacon Yard, the client receives a round phase name as unknown and checks it one exact string at a time before using it:

local function parsePhase(value: unknown): RoundPhase?
	if value == "Idle" then
		return "Idle"
	elseif value == "Active" then
		return "Active"
	elseif value == "Cooldown" then
		return "Cooldown"
	end
	return nil

Read the first line slowly. It takes unknown and returns RoundPhase?: a valid phase, or nil. Everything in this lesson is a way of writing, checking or relying on lines like that.

Roblox Studio
Studio's script editor showing src/shared/Types.luau: the strict comment, export type RoundPhase, Profile, ShopItem and FeedbackKind, and return {} at the end.

Select a numbered pin, or a row below, to read what it marks.

Beacon Yard's Types.luau, the shared names for data passed between server and client.

Choose one

Types.luau ends with return {}. What does a script get at run time when it requires Types?

Types.luau ends with return {}. What does a script get at run time when it requires Types?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 12

Three modes, one project setting

Luau's type checker works in three modes, including strict modeStrict modeThe Luau type-checking mode that infers a type for everything and reports anything that does not fit.Think of it as an inspector who reads every jar label before the kitchen opens., set by a comment on the first line of a file1:

  • --!nocheck turns checking off for the file.
  • --!nonstrict reports only things that are certainly wrong. It is the default for a script with no comment, which comes from the Workspace.LuauTypeCheckMode property (Roblox's templates set it to Nonstrict).
  • --!strict infers a type for everything and reports anything that does not fit.

Beacon Yard uses strict everywhere: every file starts with --!strict, and the project's .luaurc sets "languageMode": "strict" so editors treat new files the same way.

Both sidesRemember: Type annotations is labels on jars.

Choose all that apply

In a --!strict file, which of these does the checker report before you press Play?

In a --!strict file, which of these does the checker report before you press Play?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 12

Writing types

Annotations go after a colon. The common shapes:

  • primitives: number, string, boolean, and nil
  • optional: Player? means a Player or nil
  • lists: { BasePart }; dictionaries: { [Player]: number }
  • table shapes: { coins: number, speedOwned: boolean }
  • functions: (player: Player, amount: number) -> boolean
  • unions: string | number, and singletons such as "Idle" | "Active"

type Name = ... gives a type a name, and export type makes it usable from other files as Types.Name. Annotations do nothing at run time, so the cell below runs exactly as it would without them.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Find the broken line

Under --!strict, the checker reports an error in this fragment. Which line?

Under --!strict, the checker reports an error in this fragment. Which line?

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 12

unknown, refinement and casts

unknown means "could be anything, and you must find out before using it". You cannot index it, add to it or pass it on as anything more specific until you check it. Checks narrow the type inside their branch; this is called refinementType refinementNarrowing a value's type inside a branch by checking it, for example with typeof or ==.Think of it as opening the jar and looking before you trust the label.. typeof(x) == "number", x == "Idle", x ~= nil and x:IsA("BasePart") all refine.

:: is a cast: value :: number tells the checker "trust me". It changes nothing at run time. Casting a trusted internal module is fine, whereas casting untrusted input from a client hides the exact bug strict mode is there to catch.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Choose one

A server buy handler is written function(player: Player, item: unknown). What would change if it were item: ShopItem instead?

A server buy handler is written function(player: Player, item: unknown). What would change if it were item: ShopItem instead?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 12

typeof in a type

Sometimes the easiest way to name a type is to point at a value. In a type position, typeof(expr) means "the type the checker inferred for this expression". Two places you will see it:

  • type ConfigShape = typeof(Config) gives every field of a config table its inferred type, so you never write the list twice.
  • type Drone = typeof(setmetatable({} :: { health: number }, Drone)) names the type of a class's objects, as in the objects lesson.

Do not confuse it with the runtime function typeof(value), which returns a string such as "Vector3" while the game runs. Same word, two jobs: one for the checker, one for running code.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output

Link to this step · This confused me

Step 6 of 12

Generics: one function, many types

Some functions do not care what they hold. "Return the first item that matches" works the same for a list of prices and a list of pads. A genericGenericA type or function with a placeholder type, written in angle brackets, that is filled in where it is used.Think of it as a box with a label that says 'the same kind of thing that went in'. function names a placeholder type in angle brackets, and the checker fills it in at each call2:

function firstWhere<T>(list: { T }, predicate: (T) -> boolean): T?

Called with a { number }, T is number and the result is number?. Called with { BasePart }, the result is BasePart?, and the editor knows it has a .Position. Generic types work the same way: type Pool<T> = { items: { T }, free: { T } }.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Choose one

firstWhere<T>(list: { T }, predicate: (T) -> boolean): T? is called with a list of type { Player }. What type does the checker give the result?

firstWhere<T>(list: { T }, predicate: (T) -> boolean): T? is called with a list of type { Player }. What type does the checker give the result?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 12

Type packs, at an intro level

Luau functions can take and return several values, so the type system has a name for "several types in a row": a type pack. Two patterns you will meet in Beacon Yard:

  • ...: MessageArg types every extra variadic argument as a MessageArg.
  • (Profile, boolean) as a return type means "returns a profile and a boolean flag".

Generic packs use three dots after the name: <A...>. A signal type might be type Signal<A...> = { Fire: (self: Signal<A...>, A...) -> () }, so Signal<Player, number> fires with exactly a player and a number. You will mostly read packs in library code rather than write them.

type RemoteArg = string | number | boolean | Vector3 | nil
Choose all that apply

Given fireAll(name: string, ...: RemoteArg) where RemoteArg is string | number | boolean | Vector3 | nil, which calls does the checker accept?

Given fireAll(name: string, ...: RemoteArg) where RemoteArg is string | number | boolean | Vector3 | nil, which calls does the checker accept?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 12

The new type solver

The checker itself was rebuilt. Luau's new type solver gives better inference, fewer false errors, read-only table properties and type functions. Roblox announced its general release on the DevForum on 20 November 20253. That post says:

  • It is on by default for nocheck and nonstrict scripts in Studio.
  • Strict-mode Studio places opt in by setting Workspace.UseNewLuauTypeSolver to Enabled.

The post says nothing about luau-lsp, the command-line checker you meet below. Its own changelog lists "analyze uses the new solver by default" as unreleased, after 1.70.1. That is the version rokit.toml pins and the newest release at the time of writing, so a pinned analyze still uses the old solver. When a release ships the change, --flag:LuauSolverV2=false keeps the old one.

Choose one

According to the general-release post, what must a strict-mode Studio place like Beacon Yard set to use the new solver?

According to the general-release post, what must a strict-mode Studio place like Beacon Yard set to use the new solver?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 9 of 12

Check types outside Studio

Studio checks the file you have open. A team wants every file checked on every change, with no Studio at all. luau-lspluau-lspThe Luau language server: autocomplete, hover docs and type errors in an external editor.Think of it as studio's script editor smarts, inside VS Code., the language server behind the VS Code extension, has a command-line mode for that: luau-lsp analyze4. It reads a Rojo sourcemap to understand where each file lives in the game, and a definitions file to know Roblox's API.

Beacon Yard's m14-ship milestone already wires this into CI: rokit.toml pins luau-lsp@1.70.1, .github/workflows/ci.yml runs a types job, and scripts/typecheck.sh checks src, tests and unit (see Continuous integration: every check, blocking for the full CI pipeline).

rojo sourcemap default.project.json -o sourcemap.json
 
"$lsp" analyze \
	--sourcemap sourcemap.json \
	--definitions="$types" \
	--base-luaurc .luaurc \
	src tests

Current luau-lsp recommends naming the definitions file with --definitions:@roblox=... (unnamed --definitions= was deprecated in luau-lsp 1.56.0, though backwards compatibility remains) and downloading from luau-lsp.pages.dev to avoid GitHub rate limits. Beacon Yard's own typecheck.sh still uses the unnamed form and downloads from the pinned release on GitHub, and both work. Run wally install first, because ShopApp and ShopGui require React from Packages/, which only exists after it. Note also that in Windows PowerShell, curl is an alias for Invoke-WebRequest and has no -L flag, so use Invoke-WebRequest or curl.exe.

Your system

Windows

powershell
wally install
rojo sourcemap default.project.json -o sourcemap.json
Invoke-WebRequest -Uri "https://luau-lsp.pages.dev/type-definitions/globalTypes.None.d.luau" -OutFile "globalTypes.d.luau"
luau-lsp analyze --platform=roblox --sourcemap=sourcemap.json --definitions:@roblox=globalTypes.d.luau --base-luaurc=.luaurc src tests

macOS

bash
wally install
rojo sourcemap default.project.json -o sourcemap.json
curl -fsSL -o globalTypes.d.luau https://luau-lsp.pages.dev/type-definitions/globalTypes.None.d.luau
luau-lsp analyze --platform=roblox --sourcemap=sourcemap.json --definitions:@roblox=globalTypes.d.luau --base-luaurc=.luaurc src tests

Linux

bash
wally install
rojo sourcemap default.project.json -o sourcemap.json
curl -fsSL -o globalTypes.d.luau https://luau-lsp.pages.dev/type-definitions/globalTypes.None.d.luau
luau-lsp analyze --platform=roblox --sourcemap=sourcemap.json --definitions:@roblox=globalTypes.d.luau --base-luaurc=.luaurc src tests

When a file breaks a type, analyze names the file, its place in the game tree, the line and column, and what is wrong:

Terminal
A PowerShell window after luau-lsp analyze: an error for the client script Hud.luau at line 247, column 2, a TypeError saying the value should be Active, Cooldown or Idle but got Running.

Select a numbered pin, or a row below, to read what it marks.

luau-lsp analyze reporting a string that is not a RoundPhase.

Build along

m14-ship

  • rokit.toml
  • scripts/typecheck.sh
  • .github/workflows/ci.yml
  1. Check out m14-ship (git checkout m14-ship), run rokit install if needed, and verify luau-lsp --version.

    You should see rokit.toml lists luau-lsp and luau-lsp --version prints a version.

  2. Run wally install, then the first two commands for your system above to generate sourcemap.json and download globalTypes.d.luau.

    You should see sourcemap.json and globalTypes.d.luau exist in the repository root (both git-ignored).

  3. Run the luau-lsp analyze command for your system (the pinned 1.70.1 uses the old solver by default; add --flag:LuauSolverV2=true to compare with the new one). Then undo any test edit with git checkout -- ..

    You should see luau-lsp analyze prints no errors and exits with code 0. Temporarily changing a RoundPhase string to Running prints an error with the file and line.

Link to this step · This confused me

Step 10 of 12

Typed Luau, or TypeScript?

Typed code for Roblox

OptionWhat it isWhen pros pick itTrade-offs
Luau strict modeLuau's own checker, in Studio and luau-lsp.The default for most professional Roblox teams, and for Beacon Yard.Some patterns (classes, deep generics) still need casts; strict Studio places opt in to the new solver.
luau-lsp analyze in CIThe same checker from the command line, with Rojo paths.Teams using Rojo who want every change checked before merge.Needs a sourcemap and API definitions kept up to date.
roblox-tsWrite TypeScript; a compiler turns it into Luau.Studios with web developers, or that want TypeScript's tooling and npm packages.A build step, generated code you debug through, and a smaller pool of Roblox examples written in it.
nonstrict everywhereOnly definite errors are reported.Prototypes and game jams.Misses the type mistakes strict mode catches; harder to move to strict later.
Where does it break?“Labels on jars” is a good picture of type annotations. Where does it stop being true?

It breaks here. Labels are checked before the game runs, and only then. While it runs, nothing reads them, so a value that arrives from a client or a DataStore can be anything whatever its label says.

The true model. Annotations feed a static checker that runs on the source code. At run time Luau ignores them. Code that receives outside data marks it unknown and narrows it with typeof, comparisons or type guards before treating it as a specific type.

Choose one

CI runs luau-lsp analyze and passes. Which bug can still reach players?

CI runs luau-lsp analyze and passes. Which bug can still reach players?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 11 of 12

Challenges

Level 1, Apprentice

Challenge

Read a real signature until you could have written it.

Start from
The game at milestone m06 (download)

Done means

  • You write the full signature of DataService.trySpend from memory
  • You check it against the file and explain each part

Meet the criteria above, then record your own result. No test grades this check.

Level 2, Builder

Challenge

Write parseKind(value: unknown): FeedbackKind? in the cell below. Passing the tests completes this challenge.

Start from
The Run cell below
Check it here
Run the code cell; the challenge completes when it passes. No Studio needed.

Done means

  • parseKind accepts "coin", "buy" and "deny" and returns the exact literal
  • Wrong case, numbers and nil return nil
  • All five assertions pass
Luau
Ctrl Enter runs the cell. Checked by tests

Level 3, Professional

Challenge

Start from the m14-ship tag in the game repo and write a type-safe parser for shop items and a generic helper. You edit only challenges/luau_types_work.luau; the solution is challenges/solutions/luau-types.md in the game repo.

Start from
The game at milestone m14 (download)
Grade it
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("luau-types") in the server command bar. Copy the pass code from Output.

Done means

  • challenges/luau_types_work.luau implements parseItem(value: unknown): ShopItem? by comparing, not casting
  • firstWhere<T> returns the first item the predicate accepts, or nil
  • The Course Runner prints TEST PASS for all eleven checks and a pass code
  • luau-lsp analyze reports no errors for the file

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 12 of 12

Recap

Retiring the analogy

You no longer need “labels on jars”. Annotations feed a static checker that runs on the source code. At run time Luau ignores them. Code that receives outside data marks it unknown and narrows it with typeof, comparisons or type guards before treating it as a specific type.

Recap

  1. --!strict checks every file against its annotations before the game runs. Aliases and singleton unions (RoundPhase, ShopItem) name the data both sides share.
  2. Values from outside are unknown until code narrows them with typeof or comparisons; a :: cast silences the checker without checking anything.
  3. Generics let one function serve many types, and packs type several values in a row. The new type solver is general release, and m14-ship runs luau-lsp analyze in CI.

Type annotations is labels on jars, until labels are checked before the game runs, and only then. While it runs, nothing reads them, so a value that arrives from a client or a DataStore can be anything whatever its label says.

Next Client and server: who owns what

Link to this step · This confused me