typescript-best-practices

Enforce TypeScript best practices with strict mode, discriminated unions, and Zod validation.

1|Updated Jul 30, 2023
One-click install
npx skills add https://github.com/DanielShuguang/tool-box --skill typescript-best-practices-danielshuguang
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-best-practices
Source: https://github.com/DanielShuguang/tool-box/tree/main/.trae/skills/typescript-best-practices
Command: npx skills add https://github.com/DanielShuguang/tool-box --skill typescript-best-practices-danielshuguang

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers write more robust, maintainable, and type-safe TypeScript code by enforcing best practices and providing patterns for common scenarios.

Core Features & Use Cases

  • Type-First Development: Emphasizes defining types and interfaces before implementation.
  • Error Prevention: Utilizes discriminated unions and branded types to make illegal states unrepresentable.
  • Runtime Validation: Integrates Zod for schema definition and data validation.
  • Use Case: When refactoring a complex JavaScript codebase to TypeScript, use this Skill to guide the process of defining types, ensuring all code paths are handled, and validating external data.

Quick Start

Apply the typescript-best-practices skill to refactor the provided JavaScript code into type-safe TypeScript.

Frequently Asked Questions about typescript-best-practices

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

FAQPage Schema
How do I make illegal states unrepresentable in TypeScript?

To make illegal states unrepresentable in TypeScript, use discriminated unions and branded types to enforce type-first development. This prevents invalid data structures at compile time, ensuring robust type safety across all file operations.

What is the best way to validate runtime data in TypeScript?

The best way to validate runtime data in TypeScript is integrating Zod for schema definition and data validation. Zod bridges compile-time type safety and runtime checks, ensuring external data matches your defined schemas.

How do I refactor JavaScript code to type-safe TypeScript?

Refactor JavaScript code to type-safe TypeScript by applying type-first development principles. Define types and interfaces before implementation, enable strict mode, and implement exhaustive handling to ensure all code paths are safely covered.

Does this TypeScript refactoring approach work with React?

Yes, this TypeScript refactoring approach applies to all TypeScript and JavaScript file operations, especially when paired with React best practices. It enforces strict mode, const assertions, and exhaustive handling suitable for React components.

When do I need const assertions and exhaustive handling in TypeScript?

You need const assertions and exhaustive handling in TypeScript when enforcing strict type safety and preventing unhandled states. These techniques ensure all possible union variants are processed, making your codebase maintainable and error-resistant.