Advanced TypeScript for Harness Builders

12. Type Guards & `is` Predicates

Runtime narrowing: functions whose return type is a type predicate.

Type guards narrow a type at runtime using the is return type annotation.

File: packages/coding-agent/src/core/extensions/types.ts L820-840

export function isBashToolResult(e: ToolResultEvent): e is BashToolResultEvent {
  return e.toolName === "bash";
}
export function isReadToolResult(e: ToolResultEvent): e is ReadToolResultEvent {
  return e.toolName === "read";
}
export function isEditToolResult(e: ToolResultEvent): e is EditToolResultEvent {
  return e.toolName === "edit";
}

Structural type guard (no discriminant field):

File: packages/coding-agent/src/core/keybindings.ts L201-206

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function isLegacyKeybindingName(key: string): key is keyof typeof KEYBINDING_NAME_MIGRATIONS {
  return key in KEYBINDING_NAME_MIGRATIONS;
}

Open this chapter inside the full course