denoland/deno · error · TypeError

Illegal constructor

Error message

Illegal constructor

What it means

Window is exposed on Deno's global interface list for type and instanceof purposes only; its constructor requires the internal illegalConstructorKey (ext/web/04_global_interfaces.js:20), so new Window() throws TypeError 'Illegal constructor'. Deno has no Window instance distinct from the global object — the window global is an alias of globalThis. The class exists so library code that does feature detection (typeof window, instanceof checks) works.

Source

Thrown at ext/web/04_global_interfaces.js:20

// @ts-check
/// <reference path="../../core/internal.d.ts" />

(function () {
const { core, primordials } = __bootstrap;
const {
  Symbol,
  SymbolToStringTag,
  TypeError,
} = primordials;
const { EventTarget } = core.loadExtScript("ext:deno_web/02_event.js");

const illegalConstructorKey = Symbol("illegalConstructorKey");

class Window extends EventTarget {
  constructor(key = null) {
    if (key !== illegalConstructorKey) {
      throw new TypeError("Illegal constructor");
    }
    super();
  }

  get [SymbolToStringTag]() {
    return "Window";
  }
}

class WorkerGlobalScope extends EventTarget {
  constructor(key = null) {
    if (key != illegalConstructorKey) {
      throw new TypeError("Illegal constructor");
    }
    super();
  }

  get [SymbolToStringTag]() {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Use globalThis (or its window alias) directly — Deno's window already exists.
  2. For instanceof checks, compare against the prototype: x instanceof Window works without constructing.
  3. In tests, stub window-like behavior with your own objects rather than constructing Window.

Example fix

// before
const win = new Window(); // TypeError: Illegal constructor

// after
const win = globalThis; // `window` is an alias of globalThis in Deno
Defensive patterns

Strategy: fallback

Validate before calling

const win = typeof window === 'object' ? window : globalThis;

Prevention

When it happens

Trigger: new Window(); Reflect.construct(Window, []) — any direct instantiation from user code.

Common situations: jsdom-style test scaffolding or polyfills that construct window objects in Node habits; feature-detection code that instantiates to probe support; snippets pasted from browser DevTools experimentation.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/d48cdcc8bf7e7c6a. Report an issue: GitHub.