typed-service-contracts

Implements type-safe TypeScript services using Zod schemas and the Result pattern.

1|Updated Jun 30, 2026
One-click install
npx skills add https://github.com/khulnasoft-bot/design.md --skill typed-service-contracts-khulnasoft-bot
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: typed-service-contracts
Source: https://github.com/khulnasoft-bot/design.md/tree/main/.agents/skills/typed-service-contracts
Command: npx skills add https://github.com/khulnasoft-bot/design.md --skill typed-service-contracts-khulnasoft-bot

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires zod.

What problem does it solve? Unstructured TypeScript services often mix validation with business logic and throw unhandled exceptions, making errors unpredictable and testing difficult. This Skill enforces a strict Spec and Handler architecture where inputs are parsed into typed DTOs and errors are returned as values. ## Core Features & Use Cases - Spec and Handler Separation: Define contracts (Zod schemas, error unions, interfaces) in spec.ts and impure implementations in handler.ts. - Result Pattern Error Handling: Handlers never throw; all failures map to a discriminated union of typed error codes with recoverability flags. - Split Testing Strategy: Write data-driven contract tests for schema validation and mocked logic tests for handler behavior. - Use Case: When building a CLI that processes file paths, define a SafePathSchema that rejects traversal attempts, then implement a handler that returns FILE_NOT_FOUND results instead of crashing. ## Quick Start Ask the AI to scaffold a new service using the typed service contracts pattern with a Zod input schema, error union, and a handler that returns Result objects.

Frequently Asked Questions about typed-service-contracts

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build type-safe TypeScript services with Zod?▼

Define a spec.ts containing Zod input and output schemas, a discriminated union of error codes, and an interface. Then implement a handler.ts class that parses inputs, performs side effects, and returns Success or Failure result objects instead of throwing.

What is the Spec and Handler pattern in TypeScript?▼

It is a vertical slice architecture where the spec defines the contract (schemas, error types, interface) and the handler implements the logic. The handler catches all exceptions and maps them to typed Result values, separating validation from business logic.

How do I test Zod schemas separately from business logic?▼

Write data-driven contract tests that call safeParse with invalid inputs and assert rejection, then write handler tests that mock dependencies like fs and assert the returned Result object's error codes and success states.

Why use the Result pattern instead of throwing exceptions?▼

The Result pattern makes every failure mode explicit in the type system as a discriminated union, forcing callers to handle errors exhaustively. Handlers catch unknown runtime errors and map them to an UNKNOWN_ERROR code, preventing unhandled exceptions.

When should I not use the Spec and Handler pattern?▼

Avoid it for trivial scripts or prototypes where schema definitions and interface boilerplate outweigh the benefits. It is designed for CLIs, libraries, and high-reliability logic where input parsing and exhaustive error handling justify the structure.