You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### _`HTMLElement` νμ νꡬ_ (_An exploration into the `HTMLElement` type_)
11
11
12
-
In the 20+ years since its standardization, JavaScript has come a very long way. While in 2020, JavaScript can be used on servers, in data science, and even on IoT devices, it is important to remember its most popular use case: web browsers.
Website are made up of HTML and/or XML documents. These documents are static, they do not change. The D*ocument Object Model (DOM) is* a programming interface implemented by browsers in order to make static websites functional. The DOM API can be used to change the document structure, style, and content. The API is so powerful that countless frontend frameworks (jQuery, React, Angular, etc.) have been developed around it in order to make dynamic websites even easier to develop.
TypeScript is a typed superset of JavaScript, and it ships type definitions for the DOM API. These definitions are readily available in any default TypeScript project. Of the 20,000+ lines of definitions in _lib.dom.d.ts_, one stands out among the rest: `HTMLElement` . This type is the backbone for DOM manipulation with TypeScript.
After compiling and running the _index.html_page, the resulting HTML will be:
50
+
_index.html_νμ΄μ§λ₯Ό μ»΄νμΌνκ³ μ€νν ν, HTML κ²°κ³Ό:
51
51
52
52
```html
53
53
<divid="app">
54
54
<p>Hello, World!</p>
55
55
</div>
56
56
```
57
57
58
-
## The `Document` Interface
58
+
## `Document` μΈν°νμ΄μ€ (The `Document` Interface)
59
59
60
-
The first line of the TypeScript code uses a global variable`document` , inspecting the variable shows it is defined by the `Document` interface from the _lib.dom.d.ts_file. The code snippet contains calls to two methods, `getElementById` and `createElement`.
Pass it an element id string and it will return either `HTMLElement`or`null` . This method introduces one of the most important types, `HTMLElement`. It serves as the base interface for every other element interface. For example, the `p`variable in the code example is of type `HTMLParagraphElement`. Also take note that this method can return `null`. This is because the method can't be certain pre-runtime if it will be able to actually find the specified element or not. In the last line of the code snippet, the new _optional chaining_ operator is used in order to call `appendChild`.
This is an overloaded function definition. The second overload is simplest and works a lot like the `getElementById`method does. Pass it any `string` and it will return a standard HTMLElement. This definition is what enables developers to create unique HTML element tags.
For the first definition of `createElement`, it is using some advanced generic patterns. It is best understood broken down into chunks. Starting with the generic expression: `<KextendskeyofHTMLElementTagNameMap>`. This expression defines a generic parameter `K` that is _constrained_ to the keys of the interface `HTMLElementTagNameMap`. The map interface contains every specified HTML tag name and its corresponding type interface. For example here are the first 5 mapped values:
Some elements do not exhibit unique properties and so they just return `HTMLElement`, but other types do have unique properties and methods so they return their specific interface (which will extend from or implement `HTMLElement`).
Now, for the remainder of the `createElement`definition: `(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K]`. The first argument`tagName` is defined as the generic parameter `K` . The TypeScript interpreter is smart enough to _infer_ the generic parameter from this argument. This means that the developer does not actually have to specify the generic parameter when using the method; whatever value is passed to the `tagName` argument will be inferred as `K` and thus can be used throughout the remainder of the definition. Which is exactly what happens; the return value `HTMLElementTagNameMap[K]` takes the `tagName` argument and uses it to return the corresponding type. This definition is how the `p`variable from the code snippet gets a type of `HTMLParagraphElement`. And if the code was `document.createElement('a')`, then it would be a element of type `HTMLAnchorElement`.
0 commit comments