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