typescript-patterns
Advanced TypeScript patterns for strict mode development. Covers type utilities (Pick, Omit, Partial, Record, Awaited), generics with constraints and inference, type guards and narrowing, discriminated unions, conditional and mapped types, template literal types, const assertions, satisfies operator, module patterns, and modern JavaScript idioms (eslint-plugin-unicorn). Use when building type-safe APIs, preventing runtime errors through types, working with strict TypeScript configuration, debugging complex type errors, or enforcing modern JS idioms. Use for generics, type guards, utility types, strict mode, type inference, narrowing, type safety, const assertions, satisfies, module augmentation, unicorn, for-of, modern-js.
What this skill does
# TypeScript Patterns
## Overview
Advanced TypeScript patterns that use the type system to prevent runtime errors. Focuses on strict mode TypeScript with patterns for type inference, narrowing, and compile-time validation. **Not** a beginner tutorial.
**When to use:** Building type-safe APIs, complex data transformations, library authoring, preventing runtime errors through types, working with strict mode flags.
**When NOT to use:** Learning TypeScript basics (primitives, interfaces, classes), JavaScript-to-TypeScript migration guidance, tooling setup, or build configuration.
## Quick Reference
| Pattern | API | Key Points |
| --------------------- | ------------------------------------ | ------------------------------------ |
| Utility types | `Pick<T, K>`, `Omit<T, K>` | Extract or exclude properties |
| Partial/Required | `Partial<T>`, `Required<T>` | Make properties optional or required |
| Record type | `Record<K, V>` | Object with known keys |
| Awaited type | `Awaited<T>` | Unwrap Promise return types |
| ReturnType/Parameters | `ReturnType<F>`, `Parameters<F>` | Extract function types |
| Generic constraints | `<T extends Type>` | Constrain generic parameters |
| Type inference | `type Inferred = typeof value` | Let TypeScript infer from values |
| Type guards | `typeof`, `instanceof`, `in` | Narrow types at runtime |
| Custom type guards | `(x): x is Type` | User-defined narrowing functions |
| Inferred predicates | `(x) => x !== null` | Auto-inferred type predicate filters |
| Discriminated unions | Union with literal `type` property | Exhaustive pattern matching |
| Conditional types | `T extends U ? X : Y` | Type-level conditionals |
| Mapped types | `{ [K in keyof T]: T[K] }` | Transform all properties |
| Template literals | `` `${A}-${B}` `` | String literal type manipulation |
| Const assertions | `as const` | Narrowest possible literal types |
| Satisfies operator | `value satisfies Type` | Type check without widening |
| NoInfer utility | `NoInfer<T>` | Prevent inference from a position |
| Const type params | `<const T extends Type>` | Narrow inference without `as const` |
| Inline type imports | `import { type User }` | Import types explicitly |
| Module augmentation | `declare module 'lib' { ... }` | Extend third-party types |
| Assertion functions | `function assert(x): asserts x is T` | Throw if type guard fails |
## Common Mistakes
| Mistake | Correct Pattern |
| ------------------------------------ | ---------------------------------------------- |
| Using `any` without justification | Use `unknown` and narrow with type guards |
| Manual type assertions everywhere | Let inference work, type function returns |
| Destructuring before type narrowing | Keep object intact for discriminated unions |
| Index access without undefined check | Enable `noUncheckedIndexedAccess` |
| `as Type` casting unsafe values | Use `satisfies Type` to preserve narrow types |
| Inline objects in generics | Extract to `const` or type alias |
| Omitting `extends` in constraints | Always constrain generics when possible |
| Using `Parameters<typeof fn>[0]` | Type the parameter directly in function |
| Not handling union exhaustiveness | Use `never` checks in switch/if-else |
| `value as const satisfies Type` | Use `satisfies Type` then `as const` if needed |
## Delegation
- **Pattern discovery**: Use `Explore` agent to find existing patterns in codebase
- **Type error debugging**: Use `Task` agent for multi-step type resolution
- **Code review**: Delegate to `code-reviewer` skill for type safety audit
## References
- [Type utilities (Pick, Omit, Partial, Required, Record, Extract, Exclude, ReturnType, Parameters, Awaited)](references/type-utilities.md)
- [Generics (constraints, inference, default parameters, variance)](references/generics.md)
- [Type guards and narrowing (typeof, instanceof, in, custom guards, assertion functions)](references/type-guards.md)
- [Discriminated unions (exhaustive checking, never type, tagged unions)](references/discriminated-unions.md)
- [Conditional types (distributive conditionals, infer keyword, type-level logic)](references/conditional-types.md)
- [Mapped types (key remapping, template literals, modifiers)](references/mapped-types.md)
- [Strict mode patterns (noUncheckedIndexedAccess, exactOptionalPropertyTypes, const assertions, satisfies)](references/strict-mode.md)
- [Module patterns (inline type imports, declaration files, module augmentation, ambient types)](references/module-patterns.md)
- [Modern idioms (eslint-plugin-unicorn patterns, modern array/string/DOM APIs, ES modules)](references/modern-idioms.md)
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.