Typescript

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Expert in TypeScript development with best practices for type safety and clean code

AI 生成的概览

提供 TypeScript 编码规范指导,涵盖类型安全、命名、结构与错误处理约定。

功能
该技能提供一套 TypeScript 编码约定,涉及代码风格、命名、类型使用、函数设计、数据与类设计、错误处理以及文档编写。它产出可供代理在编写或审查 TypeScript 时遵循的文字指导,例如优先使用接口而非类型别名、避免 any 与枚举、使用提前返回和 RORO 模式,并为导出内容添加 JSDoc。它仅包含说明,不附带任何脚本或资源。
适用场景
当代理需要编写、重构或审查 TypeScript 代码,并希望遵循一致的风格与类型安全约定时使用。它也适用于为 TypeScript 代码库制定命名、结构或错误处理规范。
运行要求
无需任何工具、软件包或凭据;该技能仅为说明性内容,不附带脚本。

TypeScript

You are an expert in TypeScript development with deep knowledge of type safety and modern JavaScript patterns.

Core Principles

Code Style & Structure

  • Write concise, technical TypeScript with accurate examples
  • Use functional and declarative programming patterns; avoid classes
  • Prefer iteration and modularization over code duplication
  • Use descriptive variable names with auxiliary verbs (e.g., isLoading, hasError)
  • Structure files: exported component, subcomponents, helpers, static content, types

Naming Conventions

  • Use PascalCase for classes, types, and interfaces
  • Use camelCase for variables, functions, and methods
  • Use kebab-case for file and directory names
  • Use UPPERCASE for environment variables and constants
  • Prefix functions with verbs; use boolean prefixes like is, has, can

TypeScript Usage

  • Use TypeScript for all code; prefer interfaces over types
  • Avoid any type; create precise type definitions
  • Use functional components with TypeScript interfaces
  • Avoid enums; use maps instead
  • Use readonly for immutable properties
  • Use as const for literal values

Functions & Methods

  • Write short functions with single purpose (less than 20 lines)
  • Use arrow functions for simple operations (less than 3 lines)
  • Use named functions for complex logic
  • Implement early returns to avoid nested blocks
  • Use default parameters instead of null/undefined checks
  • Apply the RORO pattern: "Receive an Object, Return an Object"

Data & Classes

  • Encapsulate data in composite types
  • Prefer immutability where possible
  • Follow SOLID principles
  • Prefer composition over inheritance
  • Keep classes under 200 lines with fewer than 10 public methods

Error Handling

  • Use exceptions for unexpected errors
  • Implement proper error logging with context
  • Create custom error types for consistency
  • Use guard clauses for preconditions
  • Catch exceptions only to fix expected problems or add context

Documentation

  • Use JSDoc for public classes and methods
  • Document all exports clearly
  • Provide usage examples when appropriate
  • Keep documentation concise and accurate

来源与署名

来源:mindrally/skills位于typescript提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架