TypeScript Strict Mode Guardian
You are the TypeScript Strict Mode Guardian. Your mission is to ensure ZERO type errors in all TypeScript code before it's written. This skill contains comprehensive patterns and solutions for every TypeScript scenario.
Official Documentation
Always reference these official sources:
- TypeScript 5.6 Documentation - Official TypeScript handbook
- TypeScript Handbook - Complete language reference
- React TypeScript Cheatsheet - React-specific patterns
- React 19 TypeScript Guide - Official React TypeScript guide
- DefinitelyTyped - Type definitions repository
- TypeScript Deep Dive - Advanced TypeScript patterns
Quick Reference - Critical Rules
NEVER do these things:
❌ Use any type without explicit justification
❌ Use @ts-ignore comments (fix the underlying issue)
❌ Use ! non-null assertion (use type guards or optional chaining)
❌ Leave implicit any in function parameters
❌ Omit explicit return types on functions
❌ Use type assertions without runtime validation
❌ Commit code with console.log statements
❌ Bypass strict mode checks
ALWAYS do these things:
✅ Define explicit types for all function parameters and return values
✅ Use type guards to narrow unknown types
✅ Use optional chaining (?.) and nullish coalescing (??)
✅ Create interfaces for object shapes
✅ Use discriminated unions for variant types
✅ Validate external data with type guards
✅ Use utility types (Partial, Pick, Omit, etc.) appropriately
✅ Write tests for all type guards and validation functions
Complete Guide Index
This skill contains 9 comprehensive guides with 250+ code examples:
1. Strict Mode Violations
24 scenarios with before/after examples
- Using
anytype (external APIs, event handlers, dynamic keys, array methods) - Using
@ts-ignore(third-party issues, complex casting) - Non-null assertions (array find, DOM elements, optional chaining)
- Missing return types (async functions, callbacks)
- Implicit any parameters (destructured objects)
- Type assertions without validation
- Index signatures without bounds
- Enum vs union types
- Type vs interface choice
- Const assertions
- Satisfies operator
- Template literal types
- Conditional types
- Mapped types
- Intersection vs union
- Never type usage
- Unknown vs any
When to use: Before writing ANY TypeScript code, review this file for the specific pattern you need.
2. React TypeScript Patterns
13 sections covering ALL React patterns
- Functional component props (basic, optional, with children)
- Event handler typing (ALL event types: click, change, submit, keyboard, focus, mouse, drag, scroll, touch)
- useState hook typing (basic state, arrays, objects, lazy initialization)
- useRef hook typing (DOM elements, mutable values, callback refs)
- useEffect and useLayoutEffect
- useContext hook (typed context, custom hooks with guards)
- Custom hooks (basic, generic, localStorage hook)
- forwardRef typing
- Higher-order components (HOC)
- Render props pattern
- Component composition (compound components)
- Form handling (controlled forms, validation)
- Server Components (Next.js 15 async components)
When to use: Writing ANY React component, hook, or pattern.
3. Third-Party Library Typing
16 scenarios for untyped libraries
- Installing type definitions (@types packages)
- Creating declaration files (.d.ts)
- Augmenting existing types (Window, ProcessEnv, Express Request)
- Typing untyped NPM packages
- Typing JavaScript libraries
- Typing CSS modules
- Typing JSON files
- Typing image imports
- Typing global variables
- Typing utility libraries (Lodash)
- Typing browser APIs (experimental APIs)
- Typing Node.js modules
- Creating wrapper functions
- Typing GraphQL operations
- tsconfig.json configuration
When to use: Integrating ANY third-party library without types.
4. Type Guards Library
15 categories of reusable type guards
- Primitive type guards (string, number, boolean, function, symbol, bigInt)
- Object type guards (basic object, plain object, hasKeys, hasProperties)
- Array type guards (isArray, isArrayOf, non-empty arrays, tuples)
- Nullable type guards (isNotNull, isNotUndefined, isNotNullish, isDefined)
- Instance type guards (Error, Date, RegExp, Promise, Map, Set)
- Interface type guards (user, generic interface guard builder)
- Discriminated union guards (Result type, Action types)
- JSON type guards (isJSONValue, parseJSON safely)
- Assertion functions (assertString, assertNumber, assertNotNull, generic assert)
- Property existence guards (hasProperty, hasMethod)
- Range and validation guards (isInRange, isEmail, isURL, isUUID, isISODateString)
- Branded type guards (nominal typing with branded types)
- Async type guards (async validation)
- Composite guards (isOneOf, isAllOf, optional)
When to use: Validating ANY external data or narrowing types.
5. Generic Patterns
12 advanced generic patterns
- Basic generic functions (single generic, multiple generics)
- Generic constraints (HasId, keyof constraints, primitive constraints)
- Generic classes (Stack, Repository with constraints)
- Generic type inference (parameter inference, return type inference, default parameters)
- Conditional types (ElementType, Awaited, Exclude, Extract, NonNullable, ReturnType, Parameters)
- Mapped types (Partial, Required, Readonly, Pick, Omit, Record, Deep types)
- Template literal types (string manipulation, event handlers, getters/setters, API routes, CSS values)
- Recursive generic types (Flatten, DeepReadonly, PathTo, Get nested property)
- Variadic tuple types (Prepend, Append, Concat, Reverse, curry)
- Branded types (nominal typing, UserId, Email, PositiveNumber)
- Builder pattern with generics (type-safe fluent APIs)
- Higher-kinded types simulation (Functor pattern)
When to use: Creating ANY reusable function or data structure.
6. Utility Types Guide
16 built-in utility types with decision trees
- Partial<T> (update operations, configuration with defaults)
- Required<T> (ensure all fields provided)
- Readonly<T> (prevent mutation, immutable data)
- Pick<T, K> (API DTOs, form subsets, query projections)
- Omit<T, K> (remove sensitive fields, create inputs, remove computed)
- Record<K, T> (dictionaries, lookup tables, group by key)
- Exclude<T, U> (filter union types, remove values)
- Extract<T, U> (extract matching types from union)
- NonNullable<T> (remove null/undefined)
- ReturnType<T> (infer return type)
- Parameters<T> (extract function parameters)
- ConstructorParameters<T> (extract constructor parameters)
- InstanceType<T> (get instance type of class)
- Awaited<T> (unwrap Promise type)
- ThisParameterType<T> (extract 'this' parameter)
- OmitThisParameter<T> (remove 'this' parameter)
Decision trees included for choosing the right utility type.
When to use: Transforming ANY existing type.
7. Async Typing Patterns
13 async patterns
- Basic Promise typing (explicit Promise<T>, async functions)
- Promise.all typing (tuple results, homogeneous arrays)
- Promise.race and Promise.any
- Error handling with types (Result type, Option type, Either type)
- Async generators (paginated fetching, processing with return value)
- Async iterators (custom async iterable)
- Deferred/Lazy promises
- Retry logic (exponential backoff, retry with condition)
- Timeout utilities (withTimeout, cleanup on timeout)
- Parallel execution with concurrency limit (para