You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
prefer-optional-chain: typeof window !== "undefined" && window.x reported; typescript-eslint excludes typeof checks on global references #1278
typescript/prefer-optional-chain treats typeof window !== "undefined" (any typeof <global> !== "undefined" / === "undefined") as a nullish guard and asks for window?.…. typescript-eslint explicitly does not: a typeof check on a reference that resolves to no declaration in the file is an invalid operand (gatherLogicalOperands.ts, the isReferenceToGlobalFunction branch under ComparisonValueType.UndefinedStringLiteral), because window?.x throws a ReferenceError where typeof window !== "undefined" && window.x returns false when the global is not declared at all (SSR, workers, Node).
Actual: three prefer-optional-chain errors (lines 2, 5, 8).
Expected (typescript-eslint on the same file, same config): none.
In internal/rules/prefer_optional_chain/prefer_optional_chain.go the OperandTypeTypeofCheck operand (around L1514) is produced for any typeof x whatever x resolves to. The reference's test is "no resolved declaration in the file" (lib globals, implicit globals and ambient declare const globals all qualify; an imported or locally declared binding does not), which here would be: the identifier's symbol has no declaration in the current source file.
Impact: 25 of the 64 one-sided prefer-optional-chain findings on one app (all typeof window|document|navigator|performance guards). Happy to send a PR with the fixture above as valid cases.
Disclosure: prepared with an AI assistant (agent session driven by me); both linters were run on the file above.
Activity
fix(prefer-optional-chain): ignore typeof guards on global references (…