{"record":{"id":"70a7ba30e7dfde14","repo":"krahets/hello-algo","slug":"index-out-of-bounds-70a7ba","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_array_and_linkedlist/my_list.ts","lineNumber":32,"sourceCode":"    /* Constructor */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    public size(): number {\n        return this._size;\n    }\n\n    /* Get list capacity */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* Update element */\n    public get(index: number): number {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        return this.arr[index];\n    }\n\n    /* Add elements at the end */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('Index out of bounds');\n        this.arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public add(num: number): void {\n        // If length equals capacity, need to expand\n        if (this._size === this._capacity) this.extendCapacity();\n        // Add new element to end of list\n        this.arr[this._size] = num;\n        this._size++;\n    }\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_array_and_linkedlist/my_list.ts#L14-L50","documentation":"Thrown by get(index) on a custom dynamic-array list (TypeScript). The guard rejects index < 0 or index >= this._size. Note the JSDoc comment mislabels get() as 'Update element'; it is actually a read accessor returning arr[index]. Capacity (backing array length) is irrelevant — only the logical size bounds valid reads.","triggerScenarios":"Calling list.get(index) with negative index, index >= list.size(), or reading a position never written. Empty list (size 0) rejects all non-negative indices too.","commonSituations":"Off-by-one in iteration (using <= instead of <); reading before add(); caching size before mutations; passing unvalidated input directly to get().","solutions":["Guard before read: if (index >= 0 && index < list.size()) { const v = list.get(index); }.","Iterate with for (let i = 0; i < list.size(); i++) (strict <).","Recompute list.size() after mutations rather than caching.","Return a default for empty/invalid indices via a wrapper instead of letting the throw propagate."],"exampleFix":"// before\nfor (let i = 0; i <= list.size(); i++) { use(list.get(i)); } // throws at i === size\n\n// after\nfor (let i = 0; i < list.size(); i++) { use(list.get(i)); }","handlingStrategy":"validation","validationCode":"function canGet(list, index) {\n  return Number.isInteger(index) && index >= 0 && index < list.size();\n}\nif (canGet(list, i)) { const v = list.get(i); }","typeGuard":"function isValidReadIndex(list, index) {\n  return typeof index === 'number' && Number.isInteger(index) && index >= 0 && index < list.size();\n}","tryCatchPattern":"try {\n  const v = list.get(i);\n} catch (e) {\n  if (e.message === 'Index out of bounds') { /* bad index */ }\n  else throw e;\n}","preventionTips":["Iterate with strict < against size(), never <=.","Re-read size() after mutations before indexing.","Validate integer type of externally-supplied indices."],"tags":["array","dynamic-array","bounds-check","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}