Function: Add Items to Inv
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 64 of 73.
Challenge
EasyYou are provided with the following from the previous challenge:
- The generic interface
InventoryItem<T>with propertiesid,quantity, anddetails - Three inventory items:
bookItem,electronicItem, andclothingItem
Create a generic function named addItem that adds a new item to an existing inventory array.
The function should:
- Use a generic type parameter
T - Accept a parameter
inventoryof typeInventoryItem<T>[] - Accept a parameter
newItemof typeInventoryItem<T> - Return a new array of type
InventoryItem<T>[]containing all existing items plus the new item - Have an explicit return type annotation
Create the following arrays and test your function:
- Create
bookInventoryas an array containing onlybookItem - Create
newBookof typeInventoryItem<{ title: string; author: string }>with:id: 4quantity: 2details: { title: "Advanced TypeScript", author: "Jane Smith" }
- Use
addItemto addnewBooktobookInventoryand store the result inupdatedBookInventory
- Create
electronicInventoryas an array containing onlyelectronicItem - Create
newElectronicof typeInventoryItem<{ brand: string; model: string }>with:id: 5quantity: 1details: { brand: "GadgetCorp", model: "Z500" }
- Use
addItemto addnewElectronictoelectronicInventoryand store the result inupdatedElectronicInventory
Print the following outputs:
- Print the length of
updatedBookInventory - Print
updatedBookInventory[1].details.title - Print
updatedBookInventory[1].details.author - Print the length of
updatedElectronicInventory - Print
updatedElectronicInventory[1].details.brand - Print
updatedElectronicInventory[1].details.model - Print
updatedElectronicInventory[0].id - Print
updatedElectronicInventory[1].quantity
Try it yourself
// Create the generic InventoryItem interface
interface InventoryItem<T> {
id: number;
quantity: number;
details: T;
}
// Create the required objects
const bookItem: InventoryItem<{ title: string; author: string }> = {
id: 1,
quantity: 5,
details: { title: "TypeScript Guide", author: "John Doe" }
};
const electronicItem: InventoryItem<{ brand: string; model: string }> = {
id: 2,
quantity: 3,
details: { brand: "TechCorp", model: "X200" }
};
const clothingItem: InventoryItem<{ size: string; color: string }> = {
id: 3,
quantity: 10,
details: { size: "M", color: "Blue" }
};
// 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);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