{"record":{"id":"c23e5fff75a3299b","repo":"krahets/hello-algo","slug":"error-c23e5f","errorCode":null,"errorMessage":"スタックが空です","messagePattern":"スタックが空です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts","lineNumber":39,"sourceCode":"    }\n\n    /* スタックが空かどうかを判定 */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* プッシュ */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* ポップ */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('スタックが空です');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* スタックトップの要素にアクセス */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('スタックが空です');\n        return this.stackPeek.val;\n    }\n\n    /* 連結リストを Array に変換して返す */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts#L21-L57","documentation":"Thrown by pop() on the linked-list-backed stack when `this.stackPeek` is null. Defensive duplicate: pop() calls peek() first (which throws the same message when stackPeek is null), so reaching this line implies an invariant violation where stkSize is non-zero but stackPeek is null.","triggerScenarios":"Calling pop() on an empty stack (peek throws first with the same message); externally mutating stackPeek/stkSize so they disagree; subclassing and breaking the head⇔size invariant.","commonSituations":"Normal empty-stack pop is caught by peek's check; this pop-level throw signals internal-state corruption. Reaching it usually means direct field manipulation outside the class API.","solutions":["Guard pop() with `if (stack.size > 0)` — this routes through peek and avoids the throw under normal use.","Keep stackPeek and stkSize consistent; do not mutate them externally.","In subclasses, preserve stackPeek===null ⇔ stkSize===0."],"exampleFix":"// before\nconst v = stack.pop();\n\n// after\nconst v = stack.size > 0 ? stack.pop() : undefined;","handlingStrategy":"validation","validationCode":"// Guard linked-list stack pop (peek throws first with the same message).\nif (stack.size > 0) {\n  const v = stack.pop();\n}","typeGuard":"function linkedStackCanPop(stack) {\n  return stack.size > 0;\n}","tryCatchPattern":"try {\n  const v = stack.pop();\n} catch (e) {\n  if (e instanceof Error && e.message === 'スタックが空です') {\n    // empty (or invariant violated)\n  } else throw e;\n}","preventionTips":["Gate pop on `stack.size > 0` — this routes through peek and avoids the throw.","Do not mutate stackPeek/stkSize externally; keep them consistent.","Preserve stackPeek===null ⇔ stkSize===0 in subclasses.","Reaching this line with size>0 indicates internal-state corruption."],"tags":["stack","linked-list","empty-state","invariant","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}