Recap: Fine-Tuning Types
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 80 of 80.
Challenge
MediumChange a setting according to its kind.
- Write
local function processValue(config: Config, key: string): stringthat callsvalueType(config, key)first. - Return
string.upper(value :: string)for a string,tostring((value :: number) * 10)for a number, otherwise"missing". - Print
processValue(settings, "theme")andprocessValue(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"))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos8Enums, the Luau Way
The Enum Pattern in LuauNumeric Enums with TablesString Enums as UnionsUsing Literal Union EnumsFreezing Constant TablesRecap: Enums, the Luau Way11Advanced Topics
Type AssertionsType Guards With typeofThe never TypeNil Safety In Strict ModeIndex SignaturesRecap: Config Value TypesRecap: Fine-Tuning Types3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler