Labsco
google-labs-code logo

typed-service-contracts

24,800

by google-labs-code · part of google-labs-code/design.md

Architecture standard for building robust, type-safe TypeScript services using the "Spec and Handler" pattern. Use when building CLIs, libraries, or complex…

🔥🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 5 skills in the google-labs-code/design.md package — works on its own, and pairs well with its siblings.

Architecture standard for building robust, type-safe TypeScript services using the "Spec and Handler" pattern. Use when building CLIs, libraries, or complex…

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.


name: typed-service-contracts description: Architecture standard for building robust, type-safe TypeScript services using the "Spec and Handler" pattern. Use when building CLIs, libraries, or complex business logic.

Typed Service Contracts (Spec & Handler Pattern)

This skill defines a Vertical Slice Architecture backed by Design by Contract (DbC) principles. It treats application logic as rigorously defined Units of Work where inputs are parsed (not just validated) and errors are treated as values (Result Pattern) rather than exceptions.

When to use this skill

  • Building CLIs or Libraries: When you need strict boundaries between user input and system logic.
  • Complex Validation: When inputs require transformation (parsing) before being useful (e.g., ensuring a string is a valid file path).
  • High-Reliability Requirements: When you cannot afford unhandled runtime exceptions and need exhaustive error handling.
  • Testing Focus: When you want to separate data validation tests from business logic tests.

Architecture Components

1. The Spec (spec.ts)

The "Contract" or "Port". It defines the What. It must contain:

  • Input Schema: A Zod schema that parses raw input into a valid DTO.
  • Output Schema: A Zod schema defining the successful data structure.
  • Error Schema: A discriminated union of specific failure modes (not generic errors).
  • Result Type: A DiscriminatedUnion of Success | Failure.
  • Interface: The capability definition (e.g., interface ConfigureSpec).

2. The Handler (handler.ts)

The "Implementation" or "Adapter". It defines the How. It must:

  • Implement the Interface defined in the Spec.
  • Be an "Impure" class that handles side effects (File System, API calls).
  • NEVER throw exceptions. It must catch internal errors and map them to the Result type.