Project: Generic Inventory Ite
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 63 of 73.
Welcome to your first TypeScript project! You're about to build a generic inventory management system that demonstrates the real-world power of generics. This project will show you how generics can create flexible, reusable code that works with different types of data while maintaining full type safety.
The foundation of any inventory system is defining what an inventory item looks like. In a traditional approach, you might create separate interfaces for books, electronics, clothing, and other product types. But with generics, you can create one flexible structure that adapts to any type of product details.
Your first task is to create a generic interface called InventoryItem<T>. This interface will represent the common structure that all inventory items share: they need an id for identification, a quantity to track stock levels, and a details property that can hold any type of product-specific information.
The magic happens with the generic type parameter T. This allows the details property to be completely flexible - it could be book information, electronic specifications, or any other product data - while the id and quantity remain consistently typed as numbers across all inventory items.
Challenge
EasyCreate a generic interface named InventoryItem that represents the structure of any inventory item in your management system.
The interface should:
- Use a generic type parameter
T - Have an
idproperty of typenumber - Have a
quantityproperty of typenumber - Have a
detailsproperty of typeT
Create the following objects using your generic interface:
bookItemof typeInventoryItem<{ title: string; author: string }>with:id: 1quantity: 5details: { title: "TypeScript Guide", author: "John Doe" }
electronicItemof typeInventoryItem<{ brand: string; model: string }>with:id: 2quantity: 3details: { brand: "TechCorp", model: "X200" }
clothingItemof typeInventoryItem<{ size: string; color: string }>with:id: 3quantity: 10details: { size: "M", color: "Blue" }
Print the following outputs:
- Print
bookItem.id - Print
bookItem.quantity - Print
bookItem.details.title - Print
bookItem.details.author - Print
electronicItem.details.brand - Print
electronicItem.details.model - Print
clothingItem.details.size - Print
clothingItem.details.color
Try it yourself
// TODO: Write your code here
// Create the InventoryItem interface and the required objects
// Print the required outputs
console.log(bookItem.id);
console.log(bookItem.quantity);
console.log(bookItem.details.title);
console.log(bookItem.details.author);
console.log(electronicItem.details.brand);
console.log(electronicItem.details.model);
console.log(clothingItem.details.size);
console.log(clothingItem.details.color);This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs7Project: A Simple Task List
Project: Defining Task StructFunction to Add a Task10Project: Inventory Management
Project: Generic Inventory IteFunction: Add Items to Inv2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples