← Lua Academy

Typed Lua · 38 / 69

Describe nested data

YOUR TASK

Make this happen

3 steps
  1. Types can name nested records and arrays. Read the address from a typed customer record rather than duplicating its fields in every function.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

type Address = { city: string }
type Customer = { name: string, address: Address }
exports.city = function(customer: Customer): string
  return customer.name
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("nested field", function()
  local s = subject.load()
  t.expect(s.city({ name = "Ada", address = { city = "Berlin" } })):eq("Berlin")
end)