typescript-refactor
33
总安装量
4
周安装量
#11099
全站排名
安装命令
npx skills add https://github.com/pproenca/dot-skills --skill typescript-refactor
Agent 安装分布
claude-code
4
amp
3
opencode
3
kimi-cli
3
codex
3
github-copilot
3
Skill 文档
TypeScript Refactor Best Practices
Comprehensive TypeScript refactoring and modernization guide designed for AI agents and LLMs. Contains 43 rules across 8 categories, prioritized by impact to guide automated refactoring, code review, and code generation.
When to Apply
Reference these guidelines when:
- Refactoring TypeScript code for type safety and maintainability
- Designing type architectures (discriminated unions, branded types, generics)
- Narrowing types to eliminate unsafe
ascasts - Adopting modern TypeScript 4.x-5.x features (
satisfies,using, const type parameters) - Optimizing compiler performance in large codebases
- Implementing type-safe error handling patterns
- Reviewing code for TypeScript quirks and pitfalls
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type Architecture | CRITICAL | arch- |
| 2 | Type Narrowing & Guards | CRITICAL | narrow- |
| 3 | Modern TypeScript | HIGH | modern- |
| 4 | Generic Patterns | HIGH | generic- |
| 5 | Compiler Performance | MEDIUM-HIGH | compile- |
| 6 | Error Safety | MEDIUM | error- |
| 7 | Runtime Patterns | MEDIUM | perf- |
| 8 | Quirks & Pitfalls | LOW-MEDIUM | quirk- |
Quick Reference
1. Type Architecture (CRITICAL)
arch-discriminated-unionsâ Use discriminated unions over string enums for exhaustive pattern matchingarch-branded-typesâ Use branded types for domain identifiers to prevent value mix-upsarch-satisfies-over-annotationâ Usesatisfiesfor config objects to preserve literal typesarch-interfaces-over-intersectionsâ Extend interfaces instead of intersecting types for better error messagesarch-const-assertionâ Useas constfor immutable literal inferencearch-readonly-by-defaultâ Default to readonly types for function parameters and return valuesarch-avoid-partial-abuseâ AvoidPartial<T>abuse for builder patterns
2. Type Narrowing & Guards (CRITICAL)
narrow-custom-type-guardsâ Write custom type guards instead of type assertionsnarrow-assertion-functionsâ Use assertion functions for precondition checksnarrow-exhaustive-switchâ Enforce exhaustive switch withnevernarrow-in-operatorâ Narrow with theinoperator for interface unionsnarrow-eliminate-as-castsâ Eliminateascasts with proper narrowing chainsnarrow-typeof-chainsâ Usetypeofnarrowing before property access
3. Modern TypeScript (HIGH)
modern-using-keywordâ Use theusingkeyword for resource cleanupmodern-const-type-parametersâ Use const type parameters for literal inferencemodern-template-literal-typesâ Use template literal types for string patternsmodern-noinfer-utilityâ UseNoInferto control type parameter inferencemodern-accessor-keywordâ Useaccessorfor auto-generated getters and settersmodern-verbatim-module-syntaxâ EnableverbatimModuleSyntaxfor explicit import types
4. Generic Patterns (HIGH)
generic-infer-over-annotateâ Let TypeScript infer instead of explicit annotationgeneric-constrain-dont-overconstrainâ Constrain generics minimallygeneric-avoid-distributive-surprisesâ Control distributive conditional typesgeneric-mapped-type-utilitiesâ Build custom mapped types for repeated transformationsgeneric-return-type-inferenceâ Preserve return type inference in generic functions
5. Compiler Performance (MEDIUM-HIGH)
compile-explicit-return-typesâ Add explicit return types to exported functionscompile-avoid-deep-recursionâ Avoid deeply recursive type definitionscompile-project-referencesâ Use project references for monorepo buildscompile-base-types-over-unionsâ Use base types instead of large union types
6. Error Safety (MEDIUM)
error-result-typeâ Use Result types instead of thrown exceptionserror-exhaustive-error-handlingâ Use exhaustive checks for typed error variantserror-typed-catchâ Type catch clause variables asunknownerror-never-for-unreachableâ Useneverto mark unreachable code pathserror-discriminated-error-unionsâ Model domain errors as discriminated unions
7. Runtime Patterns (MEDIUM)
perf-union-literals-over-enumsâ Use union literals instead of enumsperf-avoid-delete-operatorâ Avoid thedeleteoperator on objectsperf-object-freeze-constâ UseObject.freezewithas constfor true immutabilityperf-object-keys-narrowingâ AvoidObject.keystype wideningperf-map-set-over-objectâ UseMapandSetover plain objects for dynamic collections
8. Quirks & Pitfalls (LOW-MEDIUM)
quirk-excess-property-checksâ Understand excess property checks on object literalsquirk-empty-object-typeâ Avoid the{}type â it means non-nullishquirk-type-widening-letâ Prevent type widening withletdeclarationsquirk-variance-annotationsâ Use variance annotations for generic interfacesquirk-structural-typing-escapesâ Guard against structural typing escape hatches
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions â Category structure and impact levels
- Rule template â Template for adding new rules
Reference Files
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |