← Lua Academy

Typed Lua · 32 / 69

Respect named fields

YOUR TASK

Make this happen

3 steps
  1. Record fields carry their own types. A quantity should remain numeric even when it comes from a form or document.

  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

exports.line = function(): { sku: string, quantity: number }
  return { sku = "PAPER", quantity = "2" }
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("typed line", function()
  local s = subject.load()
  t.expect(s.line()):deep_eq({ sku = "PAPER", quantity = 2 })
end)