typescript-patterns
Original:🇺🇸 English
Translated
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.
2installs
Sourceoakoss/agent-skills
Added on
NPX Install
npx skill4agent add oakoss/agent-skills typescript-patternsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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 | | Extract or exclude properties |
| Partial/Required | | Make properties optional or required |
| Record type | | Object with known keys |
| Awaited type | | Unwrap Promise return types |
| ReturnType/Parameters | | Extract function types |
| Generic constraints | | Constrain generic parameters |
| Type inference | | Let TypeScript infer from values |
| Type guards | | Narrow types at runtime |
| Custom type guards | | User-defined narrowing functions |
| Inferred predicates | | Auto-inferred type predicate filters |
| Discriminated unions | Union with literal | Exhaustive pattern matching |
| Conditional types | | Type-level conditionals |
| Mapped types | | Transform all properties |
| Template literals | | String literal type manipulation |
| Const assertions | | Narrowest possible literal types |
| Satisfies operator | | Type check without widening |
| NoInfer utility | | Prevent inference from a position |
| Const type params | | Narrow inference without |
| Inline type imports | | Import types explicitly |
| Module augmentation | | Extend third-party types |
| Assertion functions | | Throw if type guard fails |
Common Mistakes
| Mistake | Correct Pattern |
|---|---|
Using | Use |
| 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 |
| Use |
| Inline objects in generics | Extract to |
Omitting | Always constrain generics when possible |
Using | Type the parameter directly in function |
| Not handling union exhaustiveness | Use |
| Use |
Delegation
- Pattern discovery: Use agent to find existing patterns in codebase
Explore - Type error debugging: Use agent for multi-step type resolution
Task - Code review: Delegate to skill for type safety audit
code-reviewer
References
- Type utilities (Pick, Omit, Partial, Required, Record, Extract, Exclude, ReturnType, Parameters, Awaited)
- Generics (constraints, inference, default parameters, variance)
- Type guards and narrowing (typeof, instanceof, in, custom guards, assertion functions)
- Discriminated unions (exhaustive checking, never type, tagged unions)
- Conditional types (distributive conditionals, infer keyword, type-level logic)
- Mapped types (key remapping, template literals, modifiers)
- Strict mode patterns (noUncheckedIndexedAccess, exactOptionalPropertyTypes, const assertions, satisfies)
- Module patterns (inline type imports, declaration files, module augmentation, ambient types)
- Modern idioms (eslint-plugin-unicorn patterns, modern array/string/DOM APIs, ES modules)