Typed inventory application
Concept and boundary
Build an inventory around stable IDs, positive integer quantities and immutable collection updates. Validate quantities and explicitly reject duplicate IDs. Static types alone cannot enforce positivity or uniqueness.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. For the AST explorer only, install the pinned typescript package locally (npm install --save-dev typescript@5.8.3).
export type Item=Readonly<{id:string;qty:number}>; export function add(items:readonly Item[],item:Item):Item[]{if(!Number.isSafeInteger(item.qty)||item.qty<1)throw Error("qty");if(items.some(x=>x.id===item.id))throw Error("duplicate");return [...items,{...item}]} export function total(items:readonly Item[]):number{return items.reduce((n,x)=>n+x.qty,0)}import {add,total} from "./inventory"; const empty:readonly import("./inventory").Item[]=[]; const items=add(add(empty,{id:"tea",qty:2}),{id:"rice",qty:3}); console.log(total(items),empty.length); try{add(items,{id:"tea",qty:1})}catch(e){console.log("duplicate rejected")} try{add(items,{id:"salt",qty:0})}catch(e){console.log("invalid rejected")}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [] }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node main.js. Expected stdout:
5 0duplicate rejectedinvalid rejectedRejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
The inventory contract requires a numeric quantity. Restore the accepted configuration/source to compare the result.
Practice and recap
Add remove-by-ID and test missing ID, invalid quantity and preservation of the original array.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Typed inventory application — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Typed inventory application?
Using the complete accepted project and the exact command in the lesson, which result is expected?
import {add,total} from "./inventory"; const empty:readonly import("./inventory").Item[]=[]; const items=add(add(empty,{id:"tea",qty:2}),{id:"rice",qty:3}); console.log(total(items),empty.length); try{add(items,{id:"tea",qty:1})}catch(e){console.log("duplicate rejected")} try{add(items,{id:"salt",qty:0})}catch(e){console.log("invalid rejected")}Why does the rejected project fail under its stated configuration?
import {add} from "./inventory"; add([],{id:"tea",qty:"2"});