@@ -300,6 +300,33 @@ let element = document->WebAPI.Document.createElement("div")
300300let node = element->WebAPI.Element.asNode
301301```
302302
303+ ### Classifying DOM values
304+
305+ Event libraries and browser APIs sometimes return a type that is broader than the value present at
306+ runtime. A React form event target, for example, does not tell ReScript whether the target is an
307+ input, select, or another element. Calling an input-specific API then requires narrowing that value.
308+
309+ Use ` classify ` instead of repeating an ` instanceof ` binding and an unchecked ` Obj.magic ` cast. The
310+ classifier performs the runtime check and returns the correctly typed value as an ` option ` .
311+
312+ ``` ReScript
313+ let readInputValue = target =>
314+ switch target->WebAPI.HTMLInputElement.classify {
315+ | Some(input) => Some(input.value)
316+ | None => None
317+ }
318+ ```
319+
320+ The classifier parameter is polymorphic, so a library-owned target can be passed directly without an
321+ identity cast to a WebAPI type first. ` Element.classify ` , ` Document.classify ` ,
322+ ` HTMLElement.classify ` , and ` HTMLInputElement.classify ` follow the same contract. Their matching
323+ ` isInstanceOf ` functions are available when only a boolean check is needed.
324+
325+ Classification uses the corresponding constructor from ` globalThis ` . It returns ` None ` rather than
326+ raising when that constructor is unavailable in a server or worker environment. Like JavaScript
327+ ` instanceof ` , it only recognizes values created in the current realm; values from another window or
328+ iframe may not match. Enable the ` WebAPI.HTML ` feature when using the HTML-specific classifiers.
329+
303330## Visual Viewport
304331
305332` Window.visualViewport ` returns a nullable ` WebAPI.VisualViewport.t ` .
0 commit comments