type-safe-monkey-patching
Installation
SKILL.md
Type-Safe Approaches to Monkey Patching
Overview
Monkey patching - adding properties to built-in objects at runtime - is a JavaScript pattern that becomes problematic in TypeScript. TypeScript doesn't know about properties you've added to window, document, or DOM elements, leading to type errors. While as any is the quick fix, it sacrifices type safety entirely. There are better approaches that maintain type checking while modeling your runtime modifications.
When to Use This Skill
- Adding global variables to
windowordocument - Attaching data to DOM elements
- Working with libraries that require global state (jQuery, D3)
- Migrating JavaScript code that uses monkey patching
- Storing application state on global objects
The Iron Rule
Never use (obj as any).property for monkey patching. Use interface augmentation or narrower type assertions that preserve type safety.