Skip to content

EventTarget dispatchEvent cannot be called with an object, contrary to what is stated in the docs #39125

Description

@aral
  • Version: 16.4.0
  • Platform: Linux
  • Subsystem: n/a

What steps will reproduce the bug?

const e = new EventTarget()
e.dispatchEvent({type: 'something'})

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

According to the docs, it should work.

event <Object> | <Event>

Dispatches the event to the list of handlers for event.type. The event may be an Event object or any object with a type property whose value is a string.

https://nodejs.org/api/events.html#events_eventtarget_dispatchevent_event

What do you see instead?

TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of Object
    at new NodeError (node:internal/errors:363:5)
    at EventTarget.dispatchEvent (node:internal/event_target:401:13)
    at REPL16:1:3
    at Script.runInThisContext (node:vm:129:12)
    at REPLServer.defaultEval (node:repl:522:29)
    at bound (node:domain:416:15)
    at REPLServer.runBound [as eval] (node:domain:427:12)
    at REPLServer.onLine (node:repl:849:10)
    at REPLServer.emit (node:events:406:35)
    at REPLServer.emit (node:domain:470:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Additional information

n/a

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.eventsIssues and PRs related to EventEmitter and the events module.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions