Generic Type Aliases
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 65 of 80.
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
EasyDescribe a box that can hold any one type.
- Write
type Box<T> = {value: T}andlocal function unbox<T>(box: Box<T>): Tthat returnsbox.value. - Declare
local scoreBox: Box<number> = {value = 77}andlocal nameBox: Box<string> = {value = "Coddy"}. - Print
unbox(scoreBox), thenunbox(nameBox).
Press Run Code: the output shows 77 and Coddy.
Try it yourself
-- 1) type Box<T> and unbox<T>(box: Box<T>): T
-- 2) scoreBox: Box<number> and nameBox: Box<string>
-- 3) print unbox of each
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 ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe '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 TablesReading a GridBuilding a Gridtable.unpackRecap: 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 FunctionsPractice on your own: Online Luau compiler