镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
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

  1. added
    eventsIssues and PRs related to EventEmitter and the events module.
    on Jun 23, 2021
  2. Ayase-252 commented on Jun 23, 2021

    @Ayase-252
    Member

    May the doc need amend?

    cc @jasnell

  3. aduh95 commented on Jun 23, 2021

    @aduh95
    Contributor

    The code snippet produces the following behavior in other JS runtimes:

    • Firefox: Uncaught TypeError: EventTarget.dispatchEvent: Argument 1 does not implement interface Event.
    • Safari: TypeError: Argument 1 ('event') to EventTarget.dispatchEvent must be an instance of Event
    • Chromium: Uncaught TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
    • Deno: Uncaught TypeError: Cannot set property 'target' of undefined

    The behavior is consistent, we should align the doc with the actual behavior.

  4. added
    docIssues and PRs related to Node.js documentation.
    good first issueIssues that are suitable for first-time contributors.
    on Jun 23, 2021
  5. RhnSharma commented on Jun 23, 2021

    @RhnSharma
    Contributor

    I would like to take this one.

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