typescript-best-practices

Enforce type-first patterns with discriminated unions and branded types in TypeScript.

Updated Dec 7, 2025
One-click install
npx skills add https://github.com/MattRib/financial_app --skill typescript-best-practices-mattrib
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-best-practices
Source: https://github.com/MattRib/financial_app/tree/main/.agents/skills/typescript-best-practices
Command: npx skills add https://github.com/MattRib/financial_app --skill typescript-best-practices-mattrib

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

TypeScript's rich type system is powerful but easy to misuse, leading to runtime errors, unclear contracts, and fragile code as projects scale. This skill promotes a strict, type-first workflow to keep data models explicit and invariant across boundaries.

Core Features & Use Cases

  • Enforce discriminated unions and explicit types to prevent invalid states.
  • Introduce branded types for domain primitives to protect API boundaries and data integrity.
  • Provide exhaustive type guards and runtime checks to validate inputs at boundaries and during refactors.

Quick Start

Turn on strict mode in tsconfig.json and model data with explicit interfaces and types. Define function signatures and contracts before implementation. Apply discriminated unions and branded types to prevent invalid states and runtime errors.

Frequently Asked Questions about typescript-best-practices

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

FAQPage Schema
How do I prevent invalid states in TypeScript using discriminated unions?

TypeScript discriminated unions prevent invalid states by enforcing explicit, mutually exclusive data shapes. Model data with a shared literal property across interfaces. This type-first approach catches unreachable states at compile time, preventing runtime errors before deployment.

What are branded types and how do they protect API boundaries?

Branded types are domain primitives that protect API boundaries by distinguishing structurally identical types at the compiler level. They prevent invalid assignments, ensuring data integrity by validating inputs at boundaries before processing.

How do I enforce exhaustive type handling during refactors?

Enforce exhaustive type handling during refactors by applying exhaustive type guards and runtime checks. This ensures all possible discriminated union cases are handled, surfacing unhandled states immediately as compiler errors.

Does this TypeScript type-first workflow require external tooling?

No external tooling is required beyond standard TypeScript tooling. Turn on strict mode in tsconfig.json and model data explicitly. Optional libraries like Zod can enhance runtime validation at boundaries.

What's the best way to validate runtime inputs across frontend and backend TypeScript?

Validate runtime inputs by applying boundary checks using optional libraries like Zod. Define function signatures and contracts before implementation to validate inputs across frontend and backend TypeScript code safely.

Why does my TypeScript code fail at runtime despite strict types?

TypeScript code fails at runtime when type-first development is misused, leading to unclear contracts and fragile code. Fix this by enforcing discriminated unions, branded types, and runtime validation across system boundaries.