Bs-webapi: Node vs. Element


#1

This code:

open Webapi.Dom;

let el = Document.getElementById("outputSpan", document);
let () = switch (el) {
  | Some(element) => Node.setTextContent(element, "It works!")
  | None => ()
};

Gives this error:

  3 │ let el = Document.getElementById("outputSpan", document);
  4 │ let () = switch (el) {
  5 │   | Some(element) => Node.setTextContent(element, "It works!")
  6 │   | None => ()
  7 │ };
  
  This has type:
    Dom.element (defined as
      Dom.eventTarget_like(Dom._node(Dom._element(Dom._baseClass))))
  But somewhere wanted:
    Webapi.Dom.Node.t (defined as
      Dom.eventTarget_like(Dom._node(Dom._baseClass)))
  
  The incompatible parts:
    Dom._element(Dom._baseClass)
    vs
    Dom._baseClass

In the ordinary JavaScript DOM world, all Elements are Nodes, so I can do something like this:

var el = document.getElementById("outputSpan");
el.textContent = "It works!";

What am I missing here, and how do I get around this error message?


#2

I know it is bad netiquette to respond to one’s own post, but It changing the code to:

  | Some(element) => Element.setTextContent(element, "It works!")

seems to work. I am still confused as to why this works, given that setTextContent is defined in Node.re, bu I won’t complain.