Skip to content

Comments

Fix TypeScript type error in close_older_issues.cjs - add type guard for error.stack access#11069

Merged
pelikhan merged 3 commits intomainfrom
copilot/fix-typescript-type-error
Jan 21, 2026
Merged

Fix TypeScript type error in close_older_issues.cjs - add type guard for error.stack access#11069
pelikhan merged 3 commits intomainfrom
copilot/fix-typescript-type-error

Conversation

Copy link
Contributor

Copilot AI commented Jan 21, 2026

TypeScript TS18046 error in CI: catch clause error variable is unknown type (TypeScript 4.4+), cannot access .stack property without type guard.

Changes

  • Added instanceof Error type guard before accessing error.stack in catch clause (line 315)
  • Matches existing pattern used in create_issue.cjs, mcp_logger.cjs, and mcp_server_core.cjs

Before/After

// Before - TypeScript error TS18046
} catch (error) {
  core.error(`  Stack trace: ${error.stack || "(not available)"}`);
}

// After - type safe
} catch (error) {
  core.error(`  Error: ${getErrorMessage(error)}`);
  if (error instanceof Error && error.stack) {
    core.error(`  Stack trace: ${error.stack}`);
  }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[CI Failure Doctor] TypeScript Type Error in close_older_issues.cjs - Accessing .stack on unknown type</issue_title>
<issue_description>## Summary
The CI workflow (run githubnext/gh-aw#21219744664) failed due to a TypeScript type safety error in close_older_issues.cjs. The code attempts to access the .stack property directly on a catch clause error variable without a type guard, which TypeScript correctly flags as unsafe.

Failure Details

Root Cause Analysis

TypeScript Error TS18046

close_older_issues.cjs:315:36 - error TS18046: 'error' is of type 'unknown'.

315       core.error(`  Stack trace: ${error.stack || "(not available)"}`);
                                       ~~~~~

Why This Fails

As of TypeScript 4.4+, catch clause variables default to unknown instead of any for better type safety. The code at line 315 attempts to access error.stack without first checking that error is actually an Error object.

Current Code (Line 311-317)

} catch (error) {
  core.info("");
  core.error(`✗ Failed to close issue #${issue.number}`);
  core.error(`  Error: ${getErrorMessage(error)}`);  // ✅ Correct - uses helper
  core.error(`  Stack trace: ${error.stack || "(not available)"}`);  // ❌ Error - direct access
  // Continue with other issues even if one fails
}

Correct Pattern Used in Other Files

All other files in the codebase correctly use type guards:

// create_issue.cjs:537 - instanceof check
core.info(`Error details: ${error instanceof Error ? error.stack : String(error)}`);

// mcp_logger.cjs:44-45 - instanceof check
if (error instanceof Error && error.stack) {
  logger.debug(`${prefix}Stack trace: ${error.stack}`);
}

// safe_inputs_mcp_server_http.cjs:299-300 - 'in' operator check
if ("stack" in error && error.stack) {
  errorLogger.debug(`Stack trace:\n${error.stack}`);
}

Recommended Fix

Option 1: Use instanceof check (recommended)

} catch (error) {
  core.info("");
  core.error(`✗ Failed to close issue #${issue.number}`);
  core.error(`  Error: ${getErrorMessage(error)}`);
  if (error instanceof Error && error.stack) {
    core.error(`  Stack trace: ${error.stack}`);
  }
  // Continue with other issues even if one fails
}

Option 2: Use 'in' operator check

core.error(`  Stack trace: ${"stack" in error && error.stack ? error.stack : "(not available)"}`);

Option 3: Remove stack trace logging (simplest)

// Just remove line 315 - the error message from getErrorMessage() is usually sufficient

Impact

  • Severity: Medium (blocks CI, but easy fix)
  • Time to Fix: ~5 minutes
  • Affected Area: JavaScript test job in CI pipeline

Prevention Strategy

For AI Coding Agents

Add to developer instructions:

## TypeScript Catch Clause Error Handling

**ALWAYS use type guards when accessing error properties in catch clauses:**

```javascript
// ❌ WRONG - TypeScript error TS18046
} catch (error) {
  console.log(error.stack);  // Error: 'error' is of type 'unknown'
}

// ✅ CORRECT - Use instanceof type guard
} catch (error) {
  if (error instanceof Error) {
    console.log(error.stack);
  }
}

// ✅ CORRECT - Use 'in' operator
} catch (error) {
  if ("stack" in error && error.stack) {
    console.log(error.stack);
  }
}

// ✅ CORRECT - Use helper function (preferred in this codebase)
} catch (error) {
  const errorMessage = getErrorMessage(error);  // Handles unknown type safely
  console.log(errorMessage);
}

Pattern used in this codebase:

  • Use getErrorMessage(error) helper from error_helpers.cjs for error messages
  • Use error instanceof Error check before accessing .stack, .message, or other Error properties
  • Always run npm run typecheck before committing JavaScript changes

### Pre-Commit Checklist

```bash
cd actions/setup/js
npm ci
npm run typecheck  # MUST pass
npm test          # MUST pass

Historical Context

This is the first occurrence of this specific error pattern in the repository. The error was introduced as part of PR #10997, which was a large commit (2570 files changed) that appears to include a bulk repository update or merge.

Investigation Metadata

  • Pattern: TYPESCRIPT_ERROR_UNKNOWN_TYPE (1st occurrence)
  • Investigation Record: /tmp/gh-aw/cache-memory/investigations/2026-01-21-21219744664.json
  • Created: 2026-01-21T17:51:25Z

🤖 AI generated by CI Failure Doctor

A...


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…guard for error.stack access

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeScript type error in close_older_issues.cjs Fix TypeScript type error in close_older_issues.cjs - add type guard for error.stack access Jan 21, 2026
Copilot AI requested a review from mnkiefer January 21, 2026 18:16
@pelikhan
Copy link
Contributor

@copilot use error helper instead to handle errors

@pelikhan pelikhan merged commit aeababf into main Jan 21, 2026
47 of 48 checks passed
@pelikhan pelikhan deleted the copilot/fix-typescript-type-error branch January 21, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CI Failure Doctor] TypeScript Type Error in close_older_issues.cjs - Accessing .stack on unknown type

3 participants