typescript - Hur importerar man ett gränssnitt från lib.dom.d.ts?


Jag behöver definiera ett Element-gränssnitt i min kod:

interface Element {
    // my declaration
}

Men jag behöver också komma åt Element-typen i lib.dom.d.ts.

Hur skulle jag kunna göra?

Svar #1

använd detta

export type DOMElement = globalThis.Element

lib.dom har redan importerat Element-typen åt dig, du kan uttryckligen komma åt den från globalThis

Svar #2

Från global.d.ts:

/*
React projects that don't include the DOM library need these interfaces to compile.
React Native applications use React, but there is no DOM available. The JavaScript runtime
is ES6/ES2015 only. These definitions allow such projects to compile with only `--lib ES6`.

Warning: all of these interfaces are empty. If you want type definitions for various properties
(such as HTMLInputElement.prototype.value), you need to add `--lib DOM` (via command line or tsconfig.json).
*/

Följ bara det, lägg till 'compilerOptions/lib/dom' i tsconfig.json.

Relaterade artiklar