Typed Lua · 33 / 69
Narrow a union
YOUR TASK
Make this happen
A union admits more than one shape. Use type(value) to distinguish text from numbers, then return a number on both paths.
Read the cases in
lesson.test.lua, then choose Check solution to see what needs to change.Update
subject.luauntil every test passes. Keep the supplied tests unchanged.
Read the starter and tests
subject.lua
exports.asNumber = function(value: number | string): number return value end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
t.it("preserves numbers", function()
local s = subject.load()
t.expect(s.asNumber(12)):eq(12)
end)
t.it("parses text", function()
local s = subject.load()
t.expect(s.asNumber("12")):eq(12)
end)
t.it("invalid text defaults", function()
local s = subject.load()
t.expect(s.asNumber("x")):eq(0)
end)