Typescript refactoring patterns
Skill autohandai/community-skills/typescript-refactoring-patterns
A collection of curated, useful, and safe skills for Autohand Code CLI Agent
npx -y skills add autohandai/community-skills --skill typescript-refactoring-patternsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 9 stars9 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Expert TypeScript refactoring patterns for cleaner, type-safe code
The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
2.5 KB, as published. Nobody here has run it
TypeScript Refactoring Patterns
Core Principles
- Type Narrowing Over Type Assertions - Use type guards and discriminated unions instead of
ascasts - Const Assertions for Literals - Use
as constfor immutable literal types - Generic Constraints - Prefer
extendsconstraints overany - Branded Types - Use branded types for domain-specific validation
Refactoring Patterns
Extract Discriminated Union
When you see multiple boolean flags, refactor to discriminated union:
// Before
interface User {
isAdmin: boolean;
isGuest: boolean;
permissions?: string[];
}
// After
type User =
| { role: 'admin'; permissions: string[] }
| { role: 'guest' }
| { role: 'member'; permissions: string[] };
Replace Conditional with Polymorphism
When you see switch statements on type, use the strategy pattern:
// Before
function process(item: Item) {
switch (item.type) {
case 'a': return processA(item);
case 'b': return processB(item);
}
}
// After
const processors: Record<ItemType, (item: Item) => Result> = {
a: processA,
b: processB,
};
const process = (item: Item) => processors[item.type](item);
Extract Type Guard
When narrowing types, create reusable type guards:
function isNonNullable<T>(value: T): value is NonNullable<T> {
return value !== null && value !== undefined;
}
// Usage
const items = array.filter(isNonNullable);
Use Branded Types for Validation
Prevent primitive obsession with branded types:
type UserId = string & { readonly brand: unique symbol };
type Email = string & { readonly brand: unique symbol };
function createUserId(id: string): UserId {
if (!isValidUuid(id)) throw new Error('Invalid user ID');
return id as UserId;
}
Code Smell Detectors
Watch for these patterns and refactor:
anytypes (replace withunknown+ type guards)- Non-null assertions
!(add proper checks) - Type assertions
as(use type guards) - Optional chaining abuse
?.?.?.(restructure data) - Index signatures without validation
Quick Wins
- Enable
strict: truein tsconfig - Use
satisfiesfor type checking without widening - Prefer
readonlyarrays and objects - Use
unknownfor external data, validate at boundaries