{"record":{"id":"61b2ec9da319fb4b","repo":"krahets/hello-algo","slug":"the-deque-is-empty-61b2ec","errorCode":null,"errorMessage":"The Deque Is Empty.","messagePattern":"The Deque Is Empty\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"codes/typescript/chapter_stack_and_queue/array_deque.ts","lineNumber":88,"sourceCode":"    /* 队首出队 */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // 队首指针向后移动一位\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* 队尾出队 */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* 访问队首元素 */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* 访问队尾元素 */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // 计算尾元素索引\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* 返回数组用于打印 */\n    toArray(): number[] {\n        // 仅转换有效长度范围内的列表元素\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/typescript/chapter_stack_and_queue/array_deque.ts#L70-L106","documentation":"Thrown by ArrayDeque.peekFirst() ('The Deque Is Empty.') when the deque holds zero elements. peekFirst reads nums[front]; on an empty deque front points at stale/uninitialized storage, so the guard prevents returning garbage. popFirst() delegates to peekFirst(), so it also surfaces here.","triggerScenarios":"Calling peekFirst() or popFirst() when queSize === 0; draining the deque in a loop without an emptiness check.","commonSituations":"Using the deque as a queue/stack and reading the front after it was drained; off-by-one in a consumer loop; calling peek before any push.","solutions":["Guard with isEmpty(): if (!deque.isEmpty()) deque.peekFirst().","Loop with while (!deque.isEmpty()).","Track element count externally and never read the front at zero."],"exampleFix":"// before\nconst head = deque.peekFirst(); // throws when empty\n// after\nconst head = deque.isEmpty() ? undefined : deque.peekFirst();","handlingStrategy":"validation","validationCode":"function safePeekFirst(deque) {\n  return deque.isEmpty() ? undefined : deque.peekFirst();\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Guard peekFirst/popFirst with isEmpty().","Use while (!deque.isEmpty()) for draining.","Do not assume upstream code left elements."],"tags":["typescript","deque","empty-state","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}