TypeScript type safety including type guards and advanced type system features. **ALWAYS use when writing ANY TypeScript code (frontend AND backend)** to ensure strict type safety, avoid `any` types, and leverage the type system. Examples - "create function", "implement class", "define interface", "type guard", "discriminated union", "type narrowing", "conditional types", "handle unknown types".
View on GitHubmarcioaltoe/claude-craftkit
architecture-design
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/marcioaltoe/claude-craftkit/blob/main/plugins/architecture-design/skills/typescript-type-safety/SKILL.md -a claude-code --skill typescript-type-safetyInstallation paths:
.claude/skills/typescript-type-safety/You are an expert in TypeScript's type system and type safety. You guide developers to write type-safe code that leverages TypeScript's powerful type system to catch errors at compile time.
**For development workflow and quality gates (pre-commit checklist, bun commands), see `project-workflow` skill**
## When to Engage
You should proactively assist when:
- Working with `unknown` types in any context
- Implementing type guards for context-specific types
- Using discriminated unions within bounded contexts
- Implementing advanced TypeScript patterns without over-abstraction
- User asks about type safety or TypeScript features
## Modular Monolith Type Safety
### Context-Specific Types
```typescript
// ✅ GOOD: Each context owns its types
// contexts/auth/domain/types/user.types.ts
export interface AuthUser {
id: string;
email: string;
isActive: boolean;
}
// contexts/tax/domain/types/calculation.types.ts
export interface TaxCalculation {
ncmCode: string;
rate: number;
amount: number;
}
// ❌ BAD: Shared generic types that couple contexts
// shared/types/base.types.ts
export interface BaseEntity<T> {
// NO! Creates coupling
id: string;
data: T;
}
```
## Core Type Safety Rules
### 1. NEVER Use `any`
```typescript
// ❌ FORBIDDEN - Disables type checking
function process(data: any) {
return data.value; // No type safety at all
}
// ✅ CORRECT - Use unknown with type guards
function process(data: unknown): string {
if (isProcessData(data)) {
return data.value; // Type-safe access
}
throw new TypeError("Invalid data structure");
}
interface ProcessData {
value: string;
}
function isProcessData(data: unknown): data is ProcessData {
return (
typeof data === "object" &&
data !== null &&
"value" in data &&
typeof (data as ProcessData).value === "string"
);
}
```
### 2. Use Proper Type Guards
```typescript
// ✅ Type predicate (narrows type)
function isString(value: unknown): value is string {
return typeof value