Project: Inventory Item
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 67 of 80.
Challenge
EasyStart the inventory with one generic item type.
- Above the items, write
type Item<T> = {id: number, name: string, data: T}. - Annotate them:
local sword: Item<{damage: number}>andlocal potion: Item<{heal: number}>. - Under
-- Try it, addprint(potion.data.heal).
Press Run Code: the output shows Iron Sword and 40.
Try it yourself
-- Add the Item<T> type here
local sword = {id = 1, name = "Iron Sword", data = {damage = 25}}
local potion = {id = 2, name = "Health Potion", data = {heal = 40}}
-- Try it
print(sword.name)
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 Functions7Project: Typed Task Manager
Project: The Task ShapeFunction to Add a Task10Project: Generic Inventory
Project: Inventory ItemFunction: Add Items2Core 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 Combos3Typed 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