Marketplace

reviewing-type-safety

TypeScript type safety patterns and best practices for maximum type coverage. Triggers: 型安全, type safety, any, unknown, 型推論, 型ガード, type guard, discriminated union, 判別可能なUnion, strictNullChecks, 型定義, 型カバレッジ, TypeScript, 暗黙のany, implicit any, 型アサーション, type assertion.

allowed_tools: Read, Grep, Glob, Task

$ インストール

git clone https://github.com/thkt/claude-config /tmp/claude-config && cp -r /tmp/claude-config/skills/reviewing-type-safety ~/.claude/skills/claude-config

// tip: Run this command in your terminal to install the skill


name: reviewing-type-safety description: > TypeScript type safety patterns and best practices for maximum type coverage. Triggers: 型安全, type safety, any, unknown, 型推論, 型ガード, type guard, discriminated union, 判別可能なUnion, strictNullChecks, 型定義, 型カバレッジ, TypeScript, 暗黙のany, implicit any, 型アサーション, type assertion. allowed-tools:

  • Read
  • Grep
  • Glob
  • Task agent: type-safety-reviewer user-invocable: false

Type Safety Review - TypeScript Best Practices

Target: Maximum type safety with minimal type gymnastics.

Type Safety Metrics

ContextTargetWarning
Type coverage95%+< 90%
Any usage0 (justified only)> 5 instances
Type assertionsMinimal> 10 instances
Implicit any0Any > 0
Strict modeAll enabledAny disabled

Section-Based Loading

SectionFileFocusTriggers
Coveragereferences/type-coverage.mdExplicit types, avoiding anyany, unknown, 型カバレッジ
Guardsreferences/type-guards.mdNarrowing, discriminated unions型ガード, type guard
Strictreferences/strict-mode.mdtsconfig, React typesstrictNullChecks, React

Quick Checklist

Type Coverage

  • All functions have explicit return types
  • All parameters are typed (no implicit any)
  • Interface/type definitions for all data structures
  • No any without explicit justification

Type Guards & Narrowing

  • Type predicates for union types (is functions)
  • Discriminated unions for related types
  • Exhaustive checking with never
  • Avoid unsafe type assertions (as)

Strict Mode

  • strictNullChecks: true
  • noImplicitAny: true
  • strictFunctionTypes: true
  • React components extend proper HTML attributes

Key Principles

PrincipleApplication
Fail FastCatch errors at compile-time, not runtime
Let TS InferDon't over-type what's already clear
Types as DocsGood types serve as documentation
Prefer unknownUse unknown over any for safer handling

Common Patterns

Type Guard Function

function isSuccess<T>(response: Response<T>): response is SuccessResponse<T> {
  return response.success === true;
}

Discriminated Union

type Action =
  | { type: "INCREMENT"; payload: number }
  | { type: "DECREMENT"; payload: number }
  | { type: "RESET" };

// Exhaustive check
function reducer(action: Action): number {
  switch (action.type) {
    case "INCREMENT":
      return action.payload;
    case "DECREMENT":
      return -action.payload;
    case "RESET":
      return 0;
    default:
      const _exhaustive: never = action;
      return _exhaustive;
  }
}

Generic Component

interface SelectProps<T> {
  value: T;
  options: T[];
  onChange: (value: T) => void;
}

function Select<T>({ value, options, onChange }: SelectProps<T>) {
  /* ... */
}

References

Core Principles

Related Skills

  • applying-code-principles - General code quality principles
  • generating-tdd-tests - Type-safe test patterns

Used by Agents

  • type-safety-reviewer - Primary consumer of this skill