Menu
CoddyTech

Recap: Fine-Tuning Types

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 80 of 80.

challenge icon

Challenge

Medium

Change a setting according to its kind.

  1. Write local function processValue(config: Config, key: string): string that calls valueType(config, key) first.
  2. Return string.upper(value :: string) for a string, tostring((value :: number) * 10) for a number, otherwise "missing".
  3. Print processValue(settings, "theme") and processValue(settings, "maxUsers").

Press Run Code: the output ends with DARK and 1000.

Try it yourself

type Config = {[string]: string | number | boolean}

local settings: Config = {
    appName = "MyApp",
    debugMode = true,
    maxUsers = 100,
    theme = "dark",
}

local function valueType(config: Config, key: string): string
    local value = config[key]
    if value == nil then
        return "missing"
    end
    return typeof(value)
end

print(valueType(settings, "appName"))
print(valueType(settings, "debugMode"))
print(valueType(settings, "timeout"))
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

Practice on your own: Online Luau compiler