{"record":{"id":"99e7d5f2010f2857","repo":"krahets/hello-algo","slug":"error-99e7d5","errorCode":null,"errorMessage":"スタックが空です","messagePattern":"スタックが空です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/typescript/chapter_stack_and_queue/array_stack.ts","lineNumber":31,"sourceCode":"\n    /* スタックの長さを取得 */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* スタックが空かどうかを判定 */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* プッシュ */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* ポップ */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('スタックが空です');\n        return this.stack.pop();\n    }\n\n    /* スタックトップの要素にアクセス */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('スタックが空です');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* Array を返す */\n    toArray() {\n        return this.stack;\n    }\n}\n\n/* Driver Code */\n/* スタックを初期化 */\nconst stack = new ArrayStack();","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/typescript/chapter_stack_and_queue/array_stack.ts#L13-L49","documentation":"Thrown by pop() on the array-backed stack when `isEmpty()` is true (stack.length === 0). Plain Error. Note the return type is `number | undefined`, but on empty it throws rather than returning undefined — a contract mismatch callers often miss.","triggerScenarios":"Calling pop() on a freshly constructed empty stack; popping more times than you pushed; popping in a loop without an isEmpty exit.","commonSituations":"Unbalanced push/pop pairs (e.g. a recursive descent with an early-return path that skips a push but still pops); assuming pop returns undefined on empty because of the `| undefined` signature; draining a stack built from filtered input that can be empty.","solutions":["Always gate with `if (!stack.isEmpty())` or `while (!stack.isEmpty())`.","Wrap in try/catch where empty is expected and recoverable.","Do not rely on the `| undefined` return type as an emptiness signal — it throws first."],"exampleFix":"// before\nconst v = stack.pop();\n\n// after\nconst v = stack.isEmpty() ? undefined : stack.pop();","handlingStrategy":"validation","validationCode":"// Guard stack pop.\nif (!stack.isEmpty()) {\n  const v = stack.pop();\n}","typeGuard":"function stackCanPop(stack) {\n  return !stack.isEmpty();\n}","tryCatchPattern":"try {\n  const v = stack.pop();\n} catch (e) {\n  if (e instanceof Error && e.message === 'スタックが空です') {\n    // empty stack\n  } else throw e;\n}","preventionTips":["Do not trust the `number | undefined` return type — pop throws on empty.","Use isEmpty() as the sole emptiness signal.","Balance push/pop in recursive-descent patterns; ensure early returns do not skip a push.","Drain with `while (!stack.isEmpty())`."],"tags":["stack","empty-state","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}