Typescript refactor
Skill ComeOnOliver/skillshub/skills/pproenca/dot-skills/typescript-refactor
π§ The right skill, one API call. AI agent skills registry with token-efficient skill resolution. 5,000+ skills from 500+ top repos.
npx -y skills add ComeOnOliver/skillshub --skill typescript-refactorAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
7.5 KB, as published. Nobody here has run it
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 |