dom-hierarchy
1
总安装量
1
周安装量
#53755
全站排名
安装命令
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill dom-hierarchy
Agent 安装分布
mcpjam
1
openhands
1
replit
1
windsurf
1
zencoder
1
Skill 文档
Understand the DOM Hierarchy
Overview
TypeScript’s DOM types accurately model the browser’s DOM hierarchy. Understanding this hierarchy – Element extends Node, HTMLElement extends Element, etc. – helps you write correct DOM code. Use the most specific type possible for better autocompletion and type safety.
When to Use This Skill
- Working with DOM APIs
- Typing element references
- Creating DOM utilities
- Handling DOM events
- Manipulating the DOM
The Iron Rule
Use the most specific DOM type possible. HTMLElement for HTML elements, HTMLInputElement for inputs, etc.
DOM Type Hierarchy
EventTarget
âââ Node
âââ Element
âââ HTMLElement
â âââ HTMLInputElement
â âââ HTMLButtonElement
â âââ etc.
âââ SVGElement
Example
// BAD: Too general
const el: Element = document.getElementById('input');
el.value; // Error: Property 'value' doesn't exist on Element
// GOOD: Specific type
const input = document.getElementById('input') as HTMLInputElement;
input.value; // OK - value exists on HTMLInputElement
// Even better: Use querySelector with type parameter
const button = document.querySelector<HTMLButtonElement>('button.submit');
button?.disabled; // Typed correctly
Reference
- Effective TypeScript, 2nd Edition by Dan Vanderkam
- Item 75: Understand the DOM Hierarchy