test-infra icon indicating copy to clipboard operation
test-infra copied to clipboard

Fix TypeScript type safety issues in GitHub runners API by replacing any types with proper interfaces

Open Copilot opened this issue 6 months ago • 2 comments

This PR addresses TypeScript type safety concerns in the GitHub runners monitoring API by replacing unsafe any types with properly defined interfaces.

Problem

The runners API endpoint (/torchci/pages/api/runners/[org].ts) was using any types when mapping GitHub API responses, which defeats the purpose of TypeScript's type safety:

// Before: Unsafe any types
const runners = response.data.runners.map((runner: any) => ({
  // ...
  labels: runner.labels.map((label: any) => ({
    // ...
  })),
}));

Solution

Added proper TypeScript interfaces that accurately represent the GitHub API response structure:

// GitHub API interfaces for proper type safety
export interface RunnerLabel {
  id?: number;
  name: string;
  type?: "read-only" | "custom";
}

export interface GitHubApiRunner {
  id: number;
  name: string;
  os: string;
  status: string;
  busy: boolean;
  labels: RunnerLabel[];
}

// Updated mapping with proper types
const runners: RunnerData[] = response.data.runners.map((runner: GitHubApiRunner) => ({
  id: runner.id,
  name: runner.name,
  os: runner.os,
  status: runner.status as "online" | "offline",
  busy: runner.busy,
  labels: runner.labels.map((label: RunnerLabel) => ({
    id: label.id,
    name: label.name,
    type: (label.type || "custom") as "read-only" | "custom",
  })),
}));

Benefits

  • Type Safety: Eliminates unsafe any types that can cause runtime errors
  • Better Developer Experience: Provides IntelliSense and autocomplete support
  • Compile-time Error Detection: Catches potential issues before deployment
  • Self-documenting Code: Clear interface contracts make the API structure explicit
  • Maintainability: Easier to modify and extend the API safely

Testing

  • TypeScript compilation passes without errors
  • ESLint linting passes with no warnings
  • Production build succeeds
  • No functional changes to API behavior

The interfaces accurately reflect the GitHub Actions API structure while maintaining full type safety throughout the codebase.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Aug 19 '25 23:08 Copilot