Skip to content

Document types in fireEvent#2108

Merged
balloob merged 3 commits intodevfrom
better-fire-event
Nov 25, 2018
Merged

Document types in fireEvent#2108
balloob merged 3 commits intodevfrom
better-fire-event

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Nov 24, 2018

I was studying TypeScript tonight and I realized that the way how we register our HTML elements, we can also register events for fireEvent. That gives us this:

image

image

Here are some mwah things about this:

  • We have a lot of events that we use no detail for. We can define it as undefined but TypeScript would still force us to pass in undefined as 3rd arg, which is lame. So I made detail optional, but that's obviously not always true.
  • Not all files that declare an event (by listening for it) are in TypeScript, so they can't define the events. Added a new file polymer-types.ts.
  • fireEvent takes detail of an event. However, since that's part of CustomEvent, we can't do some typing magic by registering our events in the generic event type map (HTMLElementEventMap). That means that if you want to get the right types going when you type addEventListener(…), you need to specify your event a 2nd time.

Example:

interface RegisterDialogParams {
  dialogShowEvent: keyof HASSDomEvents;
  dialogTag: keyof HTMLElementTagNameMap;
  dialogImport: () => Promise<void>;
}

declare global {
  // for fire event
  interface HASSDomEvents {
    "register-dialog": RegisterDialogParams;
  }
  // for add event listener
  interface HTMLElementEventMap {
    "register-dialog": HASSDomEvent<RegisterDialogParams>;
  }
}

@ghost ghost assigned balloob Nov 24, 2018
@ghost ghost added the in progress label Nov 24, 2018
declare global {
interface HTMLElementTagNameMap {
"hui-dialog-edit-card": HuiDialogEditCard;
"hui-dialog-edit-config": HuiDialogEditCard;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramkragten my fireEvent work found this bug because it verifies that the dialog tag that you try to register is a known element 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit-card is better than edit-config I think, but good thing you found it.

@balloob balloob merged commit 8ad5280 into dev Nov 25, 2018
@balloob balloob deleted the better-fire-event branch November 25, 2018 19:47
@ghost ghost removed the in progress label Nov 25, 2018
@balloob balloob mentioned this pull request Dec 5, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants