install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/typescript-refactor" ~/.claude/skills/comeonoliver-skillshub-typescript-refactor && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/typescript-refactor/SKILL.mdsource content
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
castsas - Adopting modern TypeScript 4.x-5.x features (
,satisfies
, const type parameters)using - 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 | |
| 2 | Type Narrowing & Guards | CRITICAL | |
| 3 | Modern TypeScript | HIGH | |
| 4 | Generic Patterns | HIGH | |
| 5 | Compiler Performance | MEDIUM-HIGH | |
| 6 | Error Safety | MEDIUM | |
| 7 | Runtime Patterns | MEDIUM | |
| 8 | Quirks & Pitfalls | LOW-MEDIUM | |
Quick Reference
1. Type Architecture (CRITICAL)
— Use discriminated unions over string enums for exhaustive pattern matchingarch-discriminated-unions
— Use branded types for domain identifiers to prevent value mix-upsarch-branded-types
— Usearch-satisfies-over-annotation
for config objects to preserve literal typessatisfies
— Extend interfaces instead of intersecting types for better error messagesarch-interfaces-over-intersections
— Usearch-const-assertion
for immutable literal inferenceas const
— Default to readonly types for function parameters and return valuesarch-readonly-by-default
— Avoidarch-avoid-partial-abuse
abuse for builder patternsPartial<T>
2. Type Narrowing & Guards (CRITICAL)
— Write custom type guards instead of type assertionsnarrow-custom-type-guards
— Use assertion functions for precondition checksnarrow-assertion-functions
— Enforce exhaustive switch withnarrow-exhaustive-switchnever
— Narrow with thenarrow-in-operator
operator for interface unionsin
— Eliminatenarrow-eliminate-as-casts
casts with proper narrowing chainsas
— Usenarrow-typeof-chains
narrowing before property accesstypeof
3. Modern TypeScript (HIGH)
— Use themodern-using-keyword
keyword for resource cleanupusing
— Use const type parameters for literal inferencemodern-const-type-parameters
— Use template literal types for string patternsmodern-template-literal-types
— Usemodern-noinfer-utility
to control type parameter inferenceNoInfer
— Usemodern-accessor-keyword
for auto-generated getters and settersaccessor
— Enablemodern-verbatim-module-syntax
for explicit import typesverbatimModuleSyntax
4. Generic Patterns (HIGH)
— Let TypeScript infer instead of explicit annotationgeneric-infer-over-annotate
— Constrain generics minimallygeneric-constrain-dont-overconstrain
— Control distributive conditional typesgeneric-avoid-distributive-surprises
— Build custom mapped types for repeated transformationsgeneric-mapped-type-utilities
— Preserve return type inference in generic functionsgeneric-return-type-inference
5. Compiler Performance (MEDIUM-HIGH)
— Add explicit return types to exported functionscompile-explicit-return-types
— Avoid deeply recursive type definitionscompile-avoid-deep-recursion
— Use project references for monorepo buildscompile-project-references
— Use base types instead of large union typescompile-base-types-over-unions
6. Error Safety (MEDIUM)
— Use Result types instead of thrown exceptionserror-result-type
— Use exhaustive checks for typed error variantserror-exhaustive-error-handling
— Type catch clause variables aserror-typed-catchunknown
— Useerror-never-for-unreachable
to mark unreachable code pathsnever
— Model domain errors as discriminated unionserror-discriminated-error-unions
7. Runtime Patterns (MEDIUM)
— Use union literals instead of enumsperf-union-literals-over-enums
— Avoid theperf-avoid-delete-operator
operator on objectsdelete
— Useperf-object-freeze-const
withObject.freeze
for true immutabilityas const
— Avoidperf-object-keys-narrowing
type wideningObject.keys
— Useperf-map-set-over-object
andMap
over plain objects for dynamic collectionsSet
8. Quirks & Pitfalls (LOW-MEDIUM)
— Understand excess property checks on object literalsquirk-excess-property-checks
— Avoid thequirk-empty-object-type
type — it means non-nullish{}
— Prevent type widening withquirk-type-widening-let
declarationslet
— Use variance annotations for generic interfacesquirk-variance-annotations
— Guard against structural typing escape hatchesquirk-structural-typing-escapes
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 |