Skip to content

Add TypeScript exhaustiveness check to tool dispatch switch in toolLayer.ts #29

Description

@vincentvella

Problem

The handleTool() switch statement in src/toolLayer.ts has 45 cases and a default branch that throws "Unknown tool: {name}" at runtime. There is no compile-time check that all tools listed in src/toolSpecs.ts have a corresponding handler.

Adding a new tool to toolSpecs.ts (or renaming one) without updating the switch silently compiles and only fails at runtime — either via the "Unknown tool" error path or, worse, hitting the wrong handler if case strings drift.

Proposed Approach

  1. Narrow the name parameter type in the switch to the union of all tool name string literals (derive it from toolSpecs with typeof TOOLS[number]["name"]).
  2. Add an exhaustiveness assertion on the default branch:
default: {
  const _never: never = name;
  throw new Error(`Unknown tool: ${_never}`);
}

With the name type narrowed, TypeScript will error at compile time if any case is missing — the never assignment fails to type-check.

This is a one-time safety net: every future tool addition either gets a handler or breaks bun run typecheck.

Affected Files

  • src/toolLayer.ts — default branch of the handleTool switch
  • src/toolSpecs.ts — source of the tool name union type

Metadata

Metadata

Assignees

Labels

tech-debtCode health / refactor / debt

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions