The never Type
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 70 of 73.
Every type describes a set of possible values — boolean has two, number has many. never is the type with no values at all: it marks places in the code that cannot produce a value, because they cannot happen.
Its most common use is as the return type of a function that never returns normally — one that always raises an error, or loops forever:
local function fail(message: string): never
error(message, 0)
enderror() stops execution on the spot (you know it from Lua). The second argument 0 keeps the message clean — without it, Lua prefixes the script name and line number. A caller can trap the error with pcall, which returns false plus the message instead of crashing. Note how never differs from a function with no return values: a () function finishes and hands control back; a never function never reaches its own end.
never also shows up in contradictory narrowing. If value is a string and you test typeof(value) == "number", the branch can't ever run — inside it, the checker gives value the type never: no value could possibly be both. Seeing never in a hover or an error message is the checker telling you "this situation is impossible".
Challenge
EasyBuild a small calculator that fails loudly on bad input.
- Write
fail(message: string): neverthat callserror(message, 0). - Write
handleOperation(operation: string, value: number): numberthat returnsvalue * 2for"double",value / 2for"half", and otherwise callsfailwithInvalid operation: [operation]. - Write
tryOperation(operation: string, value: number)that callshandleOperationthroughpcall: print the result on success, orError: [message]on failure.
Run these five calls in order:
tryOperation("double", 5)tryOperation("half", 8)tryOperation("triple", 3)tryOperation("double", 15)tryOperation("half", 20)
Try it yourself
-- Write code here
-- 1) fail(message: string): never — use error(message, 0)
-- 2) handleOperation(operation: string, value: number): number
-- 3) tryOperation: pcall(handleOperation, ...), print result or "Error: ..."
-- 4) run the five test calls from the task
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos8Enums, the Luau Way
The Enum Pattern in LuauNumeric Enums with TablesString Enums as UnionsUsing Literal Union EnumsFreezing Constant TablesRecap: Enums, the Luau Way11Advanced Topics
Type AssertionsType Guards With typeofThe never TypeNil Safety In Strict ModeIndex SignaturesRecap: Fine-Tuning Types3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps