Skip to content

fix: [#1845] Replace ConsoleConstructor import with indexed access type#2095

Merged
capricorn86 merged 1 commit into
capricorn86:masterfrom
YevheniiKotyrlo:fix/console-constructor-type
Mar 12, 2026
Merged

fix: [#1845] Replace ConsoleConstructor import with indexed access type#2095
capricorn86 merged 1 commit into
capricorn86:masterfrom
YevheniiKotyrlo:fix/console-constructor-type

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Update: This PR fixed the ConsoleConstructor import (TS2305) but did not fix the TS2420 reported in #1845 — I misattributed the issue. I should have filed a separate issue for the ConsoleConstructor problem rather than linking it to #1845. The actual fix for #1845 is in #2102.


Problem

VirtualConsole.ts imports ConsoleConstructor from the Node.js 'console' module:

import type { ConsoleConstructor } from 'console';

This import is emitted into the published lib/console/VirtualConsole.d.ts, causing TS2305: Module '"console"' has no exported member 'ConsoleConstructor' for consumers whose TypeScript configuration doesn't resolve Node.js built-in module types — for example, projects with skipLibCheck: false and a moduleResolution that doesn't map the 'console' specifier to @types/node's console declarations.

Reproduction

Any consumer project with skipLibCheck: false that imports something from happy-dom touching VirtualConsole's type will get:

node_modules/happy-dom/lib/console/VirtualConsole.d.ts(2,15): error TS2305: Module '"console"' has no exported member 'ConsoleConstructor'.

This is related to #1845, which reports a similar type compatibility issue with VirtualConsole.

Fix

Replace the ConsoleConstructor import with an indexed access type Console['Console']. This resolves to the same type (the Console property on the global Console interface points to ConsoleConstructor) but avoids the import from 'console' module specifier entirely.

Before

import type { ConsoleConstructor } from 'console';
public declare Console: ConsoleConstructor;

After

// No import needed
public declare Console: Console['Console'];

Verification

  • npm run compile: passes
  • npm run lint (root): passes (zero warnings)
  • npm test (packages/happy-dom): all 48 console tests pass. 4 pre-existing failures in unrelated test files (SyncFetch, ModuleURLUtility, BrowserWindow, DetachedWindowAPI) — identical on clean master.
  • Consumer project with skipLibCheck: false: zero ConsoleConstructor errors after fix (confirmed TS2305 without fix)

@capricorn86 capricorn86 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @YevheniiKotyrlo

@capricorn86
capricorn86 merged commit 82a0888 into capricorn86:master Mar 12, 2026
4 checks passed
@Spixmaster

Spixmaster commented Mar 13, 2026

Copy link
Copy Markdown

This reads like a typical LLM pull request. Convinced without doubt to solve an issue but in the end the error is the same as before.

@capricorn86

Copy link
Copy Markdown
Owner

This reads like a typical LLM pull request. Convinced without doubt to solve an issue but in the end the error is the same as before.

@Spixmaster I know it was an LLM and I know that it didn't solve #1845, but I guess getting rid of the import is a small improvement (even though it probably didn't have any real effect), so I approved it.

@YevheniiKotyrlo

YevheniiKotyrlo commented Mar 14, 2026

Copy link
Copy Markdown
Contributor Author

Hey, wanted to follow up on this — my PR didn't actually fix #1845. I was looking at the wrong error (TS2305 from the ConsoleConstructor import) while the real issue was TS2420 (missing interface members).

After digging deeper, I found that Bun's type definitions extend the global Console interface with write() and [Symbol.asyncIterator](), which aren't in @types/node or lib.dom.d.ts. That's what causes the TS2420 for Bun users with skipLibCheck: false.

I've submitted a proper fix in #2102 that adds both methods. Sorry for the confusion on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants