Combining Type Aliases
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 35 of 73.
You now have all the pieces — aliases, unions, intersections, shapes, literals. The real power appears when they reference each other. Aliases can be built from other aliases, layer by layer:
type Username = string
type UserAge = number
-- a union of two aliases
type ContactMethod = Username | UserAge
-- a shape whose fields use the aliases above
type UserProfile = {
id: number,
displayName: Username,
preferredContact: ContactMethod,
}
-- an intersection extending the shape
type AdminProfile = UserProfile & { permissions: string }Every layer stays readable: displayName: Username says far more than displayName: string, and AdminProfile is visibly "a UserProfile plus permissions". Update a base alias once, and every composed type inherits the change automatically.
Literal unions compose the same way:
type Theme = "light" | "dark"
type Mode = Theme | "auto" -- "light" | "dark" | "auto"Remember the alias is transparent: ContactMethod is still string | number underneath, so working with one means the same typeof narrowing you already know.
Challenge
EasyCreate the following aliases, each built on the previous ones:
Username=stringUserAge=numberContactMethod=Username | UserAgeUserProfile= a shape withid: number,displayName: Username,preferredContact: ContactMethodAdminProfile=UserProfile & { permissions: string }
Declare:
regularUser: UserProfile— id1, displayName"john_doe", preferredContact"john_doe"systemAdmin: AdminProfile— id2, displayName"admin", preferredContact25, permissions"full_access"
Create getContactInfo(contact: ContactMethod): string — narrow with typeof: for a string return "Contact: [contact]", for a number return "Age: [contact]".
Print, each on its own line:
getContactInfo(regularUser.preferredContact)getContactInfo(systemAdmin.preferredContact)systemAdmin.permissions
Try it yourself
-- Write code here
-- type Username = string, UserAge = number
-- type ContactMethod = Username | UserAge
-- type UserProfile = { id, displayName, preferredContact }
-- type AdminProfile = UserProfile & { permissions: string }
-- declare regularUser and systemAdmin
-- getContactInfo narrows with typeof, then print the three lines
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 Maps