9

I need to define a Element interface in my code:

interface Element {
    // my declaration
}

But, I also need to access the Element type of lib.dom.d.ts.

How could I do?

Lucas Willems
  • 6,673
  • 4
  • 28
  • 45

4 Answers4

9

use this

export type DOMElement = globalThis.Element

lib.dom has already imported Element type for you, you can explicitly access that from globalThis

user7670223
  • 116
  • 1
  • 6
1

Export your interface first to be able to import it because all definition files should export types and interfaces.

export interface Element {
    // my declaration
}

And then import as will

import {Element} from 'path'
import {Element as ABC} from 'path'
Zunaib Imtiaz
  • 2,849
  • 11
  • 20
1

From the 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).
*/

Just follow it, add 'compilerOptions/lib/dom' to the tsconfig.json.

Garry Xiao
  • 232
  • 3
  • 9
0

Thanks to Zunaib, I found the correct solution to my problem.

In a file, define:

export interface Element2 extends Element {}

then:

import { Element2 } from 'path1';
import { Element } from 'path2';
Lucas Willems
  • 6,673
  • 4
  • 28
  • 45