Niky
Niky

Reputation: 47

ReferenceError: document is not defined (Typescript)

I'm getting the following error:

"ReferenceError: document is not defined"

This happens while trying to use document.getElementByID(); when using TypeScript.

How do I fix it?

Upvotes: 4

Views: 3165

Answers (1)

Barry Michael Doyle
Barry Michael Doyle

Reputation: 10658

This means the document has the potential of being undefined.

You'll need to make a guard statement make TypeScript happy like this:

if (document) document.getElementById();

Now we can be sure that document exists before trying to call getElementById(), this prevents us from running into issue where document is undefined.

Upvotes: 0

Related Questions