AgentSkillsCN

ts-coding-standards

当你需要编写、审查或重构TypeScript代码时,这一技能将为你提供类型安全模式、错误处理、项目布局与异步编程的指导。

SKILL.md
--- frontmatter
name: ts-coding-standards
description: Use when writing, reviewing, or refactoring TypeScript code. Provides type safety patterns, error handling, project layout, and async programming guidelines.
allowed-tools: Read, Grep, Glob

TypeScript Coding Standards

This skill provides modern TypeScript coding guidelines and best practices for this project.

When to Apply

Apply these standards when:

  • Writing new TypeScript code
  • Reviewing or refactoring existing TypeScript code
  • Designing module APIs and interfaces
  • Implementing error handling strategies

Core Principles

  1. Type Safety Over Convenience - Never sacrifice type safety for shorter code
  2. Explicit Over Implicit - Make types and intentions clear
  3. Simple Over Clever - Prefer readable code over clever abstractions
  4. Fail Fast - Catch errors at compile time, not runtime

Quick Reference

Must-Use Patterns

PatternUse Case
Discriminated UnionsState machines, API responses, Result types
Branded TypesIDs, emails, validated strings
readonlyData that should not mutate
unknown in catchSafe error handling
Explicit undefined checksArray/object indexed access

Must-Avoid Anti-Patterns

Anti-PatternAlternative
any typeunknown with type guards
Throwing exceptions for control flowResult type pattern
Optional chaining without null checkExplicit narrowing
Deep folder nesting (>3 levels)Flat, feature-based structure
Implicit undefined in optional propsExplicit T | undefined

Detailed Guidelines

For comprehensive guidance, see:

tsconfig.json Strict Mode

This project uses maximum TypeScript strictness. Ensure your code compiles with:

json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "noPropertyAccessFromIndexSignature": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true
  }
}

References