Generic Type Aliases
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 61 of 73.
Functions aren't the only things that can take a type parameter — type aliases can too. That lets you describe a whole family of table shapes at once:
type Box<T> = {value: T}A Box is a table with one field, value — and T decides what the field holds. Unlike a function call, using a generic alias in an annotation means writing the concrete type in the angle brackets:
local numberBox: Box<number> = {value = 42}
local stringBox: Box<string> = {value = "Luau in a box"}Box<number> and Box<string> are two different types stamped from the same template. Put a string into a Box<number>'s value and the checker objects immediately.
Generic aliases and generic functions are made for each other. A function can accept any box and give back exactly what's inside — note how T flows from the parameter's type into the return type:
local function unbox<T>(box: Box<T>): T
return box.value
endCall unbox(stringBox) and the checker knows the result is a string — the type parameter carried the information all the way through. This alias-plus-function pattern is the foundation of the inventory project coming in the next chapter.
Challenge
EasyBuild the box pattern yourself.
Define a generic type alias named Box with a type parameter T, describing a table with a single field value of type T.
Create a generic function named unbox that:
- declares a type parameter
T - accepts one parameter named
boxof typeBox<T> - returns
box.value, with return typeT
Create these boxes:
numberBoxof typeBox<number>holding42stringBoxof typeBox<string>holding"Luau in a box"boolBoxof typeBox<boolean>holdingtrue
Print the following, each on its own line:
numberBox.value- the result of
unbox(numberBox) - the result of
unbox(stringBox) - the result of
unbox(boolBox)
Try it yourself
-- Write code here
-- 1) define type Box<T> = {value: T}
-- 2) define unbox<T>(box: Box<T>): T
-- 3) create numberBox, stringBox and boolBox
-- 4) print numberBox.value, then unbox each box
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 Combos3Typed 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 Maps6Typing Table Shapes
Inline Shape AnnotationsType Aliases for ShapesOptional PropertiesShapes vs. Loose TablesExtending ShapesAdding Methods to ShapesSelf and Colon MethodsRecap: Defining Table Shapes9Generics: A First Look
The Problem Generics SolveA Generic Identity FunctionUsing a Generic FunctionGeneric ArraysGeneric Type AliasesRecap: Generic Functions