Menu
Coddy logo textTech

Extending Shapes

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 41 of 73.

Related shapes often share fields. Every Admin is also a User; copying the shared fields into both declarations invites them to drift apart.

You met the intersection operator & when combining aliases — it works exactly the same for shapes. A & B means "has everything from A and everything from B":

type User = {name: string, age: number}
type Admin = User & {level: number}

-- Admin now requires: name, age AND level
local root: Admin = {name = "Root", age = 35, level = 9}

Where TypeScript needs a separate extends keyword for interfaces, Luau needs nothing new — intersections already do the job. You can chain several parts:

type Contact = {email: string}
type Developer = User & Contact & {yearsExperience: number}

Don't confuse & with |: a union A | B is either shape, while an intersection A & B must satisfy both at once. And since Admin has every User field, an Admin value can be passed anywhere a User is expected.

challenge icon

Challenge

Easy

Create these type aliases:

  • Employeeid (number), name (string), department (string)
  • ManagerEmployee extended (via &) with teamSize (number)
  • Contactemail (string)
  • Developer — the intersection of Employee, Contact and {yearsExperience: number}

Create:

  • teamLead of type Manager — id 101, name "Alice Johnson", department "Engineering", teamSize 8
  • dev of type Developer — id 102, name "Bob Smith", department "Engineering", email "bob@company.com", yearsExperience 5

Create a function getManagerSummary that takes a Manager and returns [name] manages [teamSize] people.

Print, each on its own line:

  1. the result of getManagerSummary(teamLead)
  2. [name] has [yearsExperience] years of experience for dev
  3. the department of teamLead
  4. the email of dev

Try it yourself

-- Write code here
-- 1) define Employee, then Manager / Contact / Developer with &
-- 2) create teamLead and dev
-- 3) write getManagerSummary
-- 4) print the four requested lines
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau