denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.82k stars 5.38k forks source link

Property 'HTMLElement' does not exist on type 'Window & typeof globalThis'.deno-ts(2339) #17588

Open dong-lufei opened 1 year ago

dong-lufei commented 1 year ago

https://deno.land/manual@v1.30.0/advanced/jsx_dom/linkedom

Run according to the document code, it can run successfully. But it seems that the type is reported by deno-ts as wrong

import { parseHTML } from "https://esm.sh/linkedom@0.14.21";

const { document, customElements, HTMLElement } = parseHTML(`<!DOCTYPE html>
  <html lang="en">
    <head>
      <title>Hello from Deno</title>
    </head>
    <body>
      <h1>Hello from Deno</h1>
      <form>
        <input name="user">
        <button>
          Submit
        </button>
      </form>
    </body>
  </html>`);

customElements.define(
  "custom-element",
  class extends HTMLElement {
    connectedCallback() {
      console.log("it works 🥳");
    }
  },
);

document.body.appendChild(document.createElement("custom-element"));
// 文档的字符串,准备发送给客户端
document.toString();

const document: any Property 'document' does not exist on type 'Window & typeof globalThis'.deno-ts(2339)

Seally commented 1 year ago

I think this is more of an issue with the package typings than with Deno. I do not expect a type definition like that to work with Deno as-is. Usually the correct typings should be included or referenced in the types package, but the types needed here are huge so....

For the time being, enabling DOM typings might work. See: https://deno.land/manual@v1.30.0/advanced/typescript/configuration#targeting-deno-and-the-browser

lilnasy commented 1 year ago

linkedom's API is not the same as the browser's, and the type declerations are innacurate. See linkedom#167.