Menu
Coddy logo textTech

Create Specific Inventory Type

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 66 of 73.

challenge icon

Challenge

Easy

You are provided with the following from the previous challenge:

  • The generic interface InventoryItem<T> with properties id, quantity, and details
  • The generic function addItem that adds items to inventory arrays
  • The generic function findItemById that searches for items by ID

Create two specific object types and their corresponding inventory item type aliases:

  • Create a type alias Book with properties:
    • title of type string
    • author of type string
  • Create a type alias Electronic with properties:
    • brand of type string
    • model of type string

Create type aliases for specific inventory items:

  • Create BookItem as InventoryItem<Book>
  • Create ElectronicItem as InventoryItem<Electronic>

Create concrete inventory items using your new type aliases:

  • Create specificBook of type BookItem with:
    • id: 100
    • quantity: 8
    • details: { title: "Clean Code", author: "Robert Martin" }
  • Create specificElectronic of type ElectronicItem with:
    • id: 200
    • quantity: 4
    • details: { brand: "Sony", model: "WH-1000XM4" }

Create typed inventory arrays:

  • Create bookStore of type BookItem[] containing specificBook
  • Create electronicStore of type ElectronicItem[] containing specificElectronic

Create additional items and test your type system:

  • Create anotherBook of type BookItem with:
    • id: 101
    • quantity: 3
    • details: { title: "Design Patterns", author: "Gang of Four" }
  • Use addItem to add anotherBook to bookStore and store in expandedBookStore

Print the following outputs:

  • Print specificBook.details.title
  • Print specificBook.details.author
  • Print specificElectronic.details.brand
  • Print specificElectronic.details.model
  • Print the length of expandedBookStore
  • Use findItemById to find item with ID 101 in expandedBookStore and print the result's details.title
  • Use findItemById to find item with ID 200 in electronicStore and print the result's quantity
  • Print expandedBookStore[1].details.author

Try it yourself

// Generic interface from previous challenge
interface InventoryItem<T> {
  id: number;
  quantity: number;
  details: T;
}

// Generic function from previous challenge
function addItem<T>(inventory: InventoryItem<T>[], item: InventoryItem<T>): InventoryItem<T>[] {
  return [...inventory, item];
}

// Create the generic findItemById function
function findItemById<T>(inventory: InventoryItem<T>[], id: number): InventoryItem<T> | undefined {
  return inventory.find(item => item.id === id);
}

// Create test inventories
const mixedBookInventory: InventoryItem<{ title: string; author: string }>[] = [
  { id: 10, quantity: 3, details: { title: "JavaScript Basics", author: "Alice Brown" } },
  { id: 11, quantity: 7, details: { title: "React Fundamentals", author: "Bob Wilson" } },
  { id: 12, quantity: 2, details: { title: "Node.js Guide", author: "Carol Davis" } }
];

const mixedElectronicInventory: InventoryItem<{ brand: string; model: string }>[] = [
  { id: 20, quantity: 5, details: { brand: "Samsung", model: "Galaxy S23" } },
  { id: 21, quantity: 1, details: { brand: "Apple", model: "iPhone 15" } }
];

// Test the function and print outputs
const result1 = findItemById(mixedBookInventory, 11);
console.log(result1 ? result1.details.title : undefined);

const result2 = findItemById(mixedElectronicInventory, 20);
console.log(result2 ? result2.details.brand : undefined);

const result3 = findItemById(mixedBookInventory, 99);
console.log(result3);

const result4 = findItemById(mixedBookInventory, 12);
console.log(result4 ? result4.quantity : undefined);

const result5 = findItemById(mixedElectronicInventory, 21);
console.log(result5 ? result5.details.model : undefined);

const result6 = findItemById(mixedBookInventory, 10);
console.log(result6 ? result6.id : undefined);

const result7 = findItemById(mixedElectronicInventory, 50);
console.log(result7);

All lessons in Introduction To TypeScript