I have the following line of code in a function:
if (!document) return;
For some reason, I am getting the following ESLint error from the official TypeScript rules:
Unnecessary conditional, value is always falsy. eslint(@typescript-eslint/no-unnecessary-condition)
It is not true that document
is always truthy. When the code runs on the server, in a Node.js program or as part of a react server component, the document
object is not available.
Is there a way to get TypeScript to understand that document
may be undefined
?
EDIT:
I intend for the code to be able to run in both a node.js (server) environment, and a browser environment. Therefore, env
in eslint config is set to { node: true, browser: true, }
and lib.dom.d.ts
is included via this tsconfig: "lib": ["ESNext", "DOM", "DOM.Iterable"],