type-safe-monkey-patching

Augment global objects and DOM elements in TypeScript without using any.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill type-safe-monkey-patching
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-safe-monkey-patching
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/type-safe-monkey-patching
Command: npx skills add https://github.com/luyi985/lyi-bash --skill type-safe-monkey-patching

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Monkey patching built-in objects at runtime often breaks TypeScript type safety. This guide shows how to model runtime modifications with type-safe techniques instead of casting to any.

Core Features & Use Cases

  • Interface augmentation for global properties like Window and Document
  • Safe patterns for attaching data to DOM elements and DOM-related globals
  • Alternatives for global state management and per-element data without sacrificing type safety
  • Guidance for migrating legacy code from unsafe monkey patching to structured approaches

Quick Start

Augment the global Window type with a defined interface and assign typed values safely.

Frequently Asked Questions about type-safe-monkey-patching

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add custom properties to the window object in TypeScript without using as any?

To add custom properties to the window object without using as any, use TypeScript interface augmentation. Define an interface that augments the global Window type, then assign typed values directly for safe global state management.

What is the best way to attach data to DOM elements in TypeScript while keeping type safety?

The best way to attach data to DOM elements while keeping type safety is using safe patterns for DOM manipulation. Define typed interfaces for your DOM data and apply safer type assertions instead of relying on unsafe monkey patching.

How do I migrate legacy code from unsafe monkey patching to structured TypeScript approaches?

To migrate legacy code from unsafe monkey patching, replace runtime modifications and as any casts with interface augmentation. Enforce structured approaches for shared global state and per-element data to restore type safety.

Why does monkey patching global objects break TypeScript type safety?

Monkey patching global objects breaks TypeScript type safety because it modifies runtime objects outside their defined interfaces. This forces developers to use unsafe type assertions like as any, bypassing the compiler's type checking.

Can I safely augment the Document interface for DOM-related globals in TypeScript?

Yes, you can safely augment the Document interface for DOM-related globals in TypeScript. Interface augmentation allows you to model runtime modifications on document or DOM nodes while enforcing type safety and avoiding loose typing.