Skip to content

CustomEvent should be parameterized by detail type. #14785

@justinfagnani

Description

@justinfagnani

TypeScript Version: 2.2.1

Code

If CustomEvent were parameterized, code that fires events could declare what the type of detail is so that listeners could be more type safe:

interface MyDetail {
  foo: string;
}

declare global {
  interface DocumentEventMap {
    'my-event': CustomEvent<MyDetail>;
  }
}

document.addEventListener('my-event', (e) => {
  e.detail.foo; // ok
  e.detail.bar; // warning
});

Metadata

Metadata

Assignees

Labels

Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions