Recap: Introduction to Luau
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 5 of 73.
Challenge
MediumCreate a variable named userId with an explicit type annotation of number and assign it the value 12345.
Create a variable named username with an explicit type annotation of string and assign it the value "john_doe".
Suppose your program contains this line, which the type checker rejects:
username = userId -- ✗ type error: number is not a stringFix it: create a new variable called userIdString of type string that converts userId using tostring(), then assign userIdString to username.
Finally, print userId and username, each on its own line.
Try it yourself
-- Write code here
-- 1) declare userId: number = 12345 and username: string = "john_doe"
-- 2) convert userId with tostring() into userIdString: string
-- 3) assign userIdString to username
-- Print the results
print(userId)
print(username)
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 ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe '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 Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps