{"record":{"id":"32a5d5bea23b5a67","repo":"denoland/deno","slug":"err-invalid-arg-type-32a5d5","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"event\" argument must be an instance of Event. Received ${actual}","messagePattern":"The \"event\" argument must be an instance of Event\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/event_target.mjs","lineNumber":704,"sourceCode":"          MapPrototypeDelete(this[kEvents], type);\n        }\n        this[kRemoveListener](root.size, type, listener, capture);\n        break;\n      }\n      handler = handler.next;\n    }\n  }\n\n  /**\n   * @param {Event} event\n   */\n  dispatchEvent(event) {\n    if (!isEventTarget(this)) {\n      throw new ERR_INVALID_THIS(\"EventTarget\");\n    }\n\n    if (!ObjectPrototypeIsPrototypeOf(globalThis.Event.prototype, event)) {\n      throw new ERR_INVALID_ARG_TYPE(\"event\", \"Event\", event);\n    }\n\n    if (event[kIsBeingDispatched]) {\n      throw new ERR_EVENT_RECURSION(event.type);\n    }\n\n    this[kHybridDispatch](event, event.type, event);\n\n    return event.defaultPrevented !== true;\n  }\n\n  [kHybridDispatch](nodeValue, type, event) {\n    const createEvent = () => {\n      if (event === undefined) {\n        event = this[kCreateEvent](nodeValue, type);\n        event[kTarget] = this;\n        event[kIsBeingDispatched] = true;\n      }","sourceCodeStart":686,"sourceCodeEnd":722,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/event_target.mjs#L686-L722","documentation":"dispatchEvent requires its argument to be a real Event: the polyfill checks ObjectPrototypeIsPrototypeOf(globalThis.Event.prototype, event). Plain objects ({ type: 'x' }), strings, or class instances that merely duck-type an Event all throw ERR_INVALID_ARG_TYPE. Subclasses of Event (including CustomEvent) pass the check.","triggerScenarios":"dispatchEvent({ type: 'ping', detail: 1 }); dispatchEvent('ping'); passing a payload object that was JSON round-tripped; a MockEvent class that implements the shape but does not extend Event; forwarding an EventEmitter-style { type, ... } object into an EventTarget.","commonSituations":"Porting EventEmitter emit('name', data) calls to EventTarget dispatch; test fixtures that fake events as literals; cross-realm or cross-copy constructs where the object came from a different context and is not a real Event instance.","solutions":["Wrap the payload: dispatchEvent(new CustomEvent('ping', { detail: data }))","Use new Event(type) when no payload is needed","Make custom event classes extend Event (or CustomEvent) so the prototype check passes","Convert { type, detail } literals at the boundary before dispatching"],"exampleFix":"// before\ntarget.dispatchEvent({ type: 'ping', detail: 42 });\n\n// after\ntarget.dispatchEvent(new CustomEvent('ping', { detail: 42 }));","handlingStrategy":"type-guard","validationCode":"function dispatch(target, typeOrEvent, detail) {\n  const ev = typeOrEvent instanceof Event\n    ? typeOrEvent\n    : detail === undefined ? new Event(typeOrEvent) : new CustomEvent(typeOrEvent, { detail });\n  target.dispatchEvent(ev);\n}","typeGuard":"const isEvent = (v) => v instanceof Event; // matches the polyfill's prototype check","tryCatchPattern":"try { target.dispatchEvent(ev); } catch (e) { if (e?.code === 'ERR_INVALID_ARG_TYPE' && /event/.test(e.message)) throw new TypeError('dispatchEvent requires an Event instance'); throw e; }","preventionTips":["Always construct new Event/CustomEvent at the dispatch boundary; never forward plain payload objects","Make custom event classes extend Event or CustomEvent so the prototype check passes","Be aware the check is prototype-based: duck-typed or cross-realm objects fail even if they look like events"],"tags":["event-target","argument-type","dispatch","node-compat","deno"],"backgroundTag":"invalid-argument-value","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}