Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated CustomEvent.detail to be null when not defined #1586

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

GauBen
Copy link

@GauBen GauBen commented Jun 29, 2023

Fixes #1585

@github-actions
Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

@GauBen
Copy link
Author

GauBen commented Jun 29, 2023

I'll update the tests accordingly if this change is accepted

@saschanaz
Copy link
Contributor

Sounds fine to me.

@saschanaz
Copy link
Contributor

Can you perhaps also add unit tests for this?

@GauBen
Copy link
Author

GauBen commented Jun 29, 2023

Is eventlistener.ts the right place for it?

@saschanaz
Copy link
Contributor

I'd prefer a separate file for custom events.

@GauBen
Copy link
Author

GauBen commented Jun 29, 2023

I just noticed that it is possible to create broken CustomEvents:
https://www.typescriptlang.org/play?#code/MYewdgziA2CmB00QHMAUZYHcAEBhArhAC4gC2AogG6xhEA8xATgJZjIB8qA5LNbVwEp4AE1hEAhs2jYAvHOxh80aAIDcAKAD0m7Lr36Dho8aMA9APxA

Do I try to address this too? Would this be a breaking change?

Edit, suggested change:

type CustomEventInit<T = any> = EventInit & (T extends {} ? { detail: T } : { detail?: T });

declare var CustomEvent: {
  prototype: CustomEvent;
  new <T>(
    ...args: T extends {}
      ? [type: string, eventInitDict: CustomEventInit<T>]
      : [type: string, eventInitDict?: CustomEventInit<T>]
  ): CustomEvent<T>;
};

@saschanaz
Copy link
Contributor

Looks right, although it starts to be over-complex... I'm okay with that if @sandersn says okay

@sandersn
Copy link
Member

I don't know CustomEvent, but running the example from the playground link, it looks like CustomEvent.default is null if CustomEventInit isn't provided to the constructor. At least, it doesn't appear to be undefined the way that { detail?: T } implies. So what about this?

interface CustomEventInit<T = any> extends EventInit {
    detail: T; // note: you probably need to add the `T extends {} ? T : null` conditional type to map undefined -> null
}

declare var CustomEvent: {
  prototype: CustomEvent;
  new<T>(type: string, eventInitDict: CustomEventInit<T>): CustomEvent<T>
  new(type: string): CustomEvent<null>
};

Of course this might break backward compatibility. I don't know how new or widely-used CustomEvent is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CustomEvent.detail is null when not defined
3 participants