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
Export typescript types for events with details #1183
Export typescript types for events with details #1183
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Thank you! This is something people have been asking for, so I really appreciate the PR. At a glance, this looks good. My only concern is since not all events have details, it's not clear to contributors and maintainers when an event should be added. I wonder if adding all events with null or undefined payloads for consistency would help with that. What do you think? Aside — I'd like to hold off on merging this until #1167 is in and #1180 is addressed. I'm hoping both will be finished up this week. Hopefully that won't create too many conflicts. |
Sure, that sounds good. All these types will just be aliases for |
I'm OK with putting them all in separate files OR putting all events (with and without details) in one file. The idea is that things are consistent and easier to maintain, even if on the surface it feels a bit inefficient. 😄 |
[Edit] Oh, haven't read the two previous comments, when I've written the comment below, anyway... @mpharoah @claviska Very happy to here that someone finally adds those SL event types 😃👍
|
I looked into that, but it didn't really seem to work. Adding the explicit I think this is because of the way it is implemented in typescript. The |
Sounds good. I'll continue with my approach of doing both (one file for each, plus an |
Actually it works: |
Sorry, I forgot to reply to this. We should identify all events that don't emit a consistent details payload and either make them consistent or, when it doesn't make sense, mark the payload as optional. |
Looks like to get it everywhere (except It also doesn't update Actually, just adding it to |
I think I'm going to switch to a single file because it will allow making |
Can
This will give us a place to add future type files. If not, throwing it in |
… event type matches a Shoelace event
Nevermind, combining them into one file was not necessary. |
Found an unused and undocumented event It's bound to an event handler, but it looks like nothing ever fires it. |
Whoops. That was removed in 2.0.0-beta.22. It's unused, so feel free to get rid of it. |
@mpharoah Matt, there's a new event type now in the |
bubbles: false, | ||
composed: false, | ||
cancelable: true | ||
cancelable: true, | ||
detail: {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed null
details to an empty object to be consistent with all other events in Shoelace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot 👍
May I ask: What's the advantage of using
type SlInvalidEvent = CustomEvent<Record<PropertyKey, never>>;
instead of just using type SlInvalidEvent = CustomEvent<{}>;
(see here)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suprisingly, {}
doesn't actually mean empty object when used as a type in Typescript. I actually initially used CustomEvent<{}>
but then got a linter error about it not doing what you expect:
error Don't use
{}
as a type.{}
actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want
object
instead.- If you want a type meaning "any value", you probably want
unknown
instead.- If you want a type meaning "empty object", you probably want
Record<string, never>
instead @typescript-eslint/ban-types
Added some comments to make it more clear what the Typescript type manipulation stuff is doing. The PR is now ready for review again. |
Thank you for this PR. You are a TypeScript ninja! I'll admit that these types scare me a bit, but your use of comments to describe what each piece is doing is superb and makes it a much easier pill to swallow. 😅 |
Updated PR description:
This pull request exports Typescript types for all Shoelace events and makes the internal
emit
function type safe to ensure all emitted events have the correct shape for theirdetail
property.Event types can be imported either individually as a default export from each event type file, or in bulk from the
events.ts
file which re-exports all events in a single file.This removes the need for consumers using TypeScript to redefine the events, and ensures that any breaking changes in Shoelace events will be detected at transpile time by consumers.
All events except for
sl-error
have a unique (or non-existent)detail
property; howeversl-error
is the exception since it has detail of shape{ status: number }
when emitted from thesl-include
component, but has no detail when emitted from other components.An unused event handler in
sl-dropdown
was also removed as per this comment.Original PR description:
This merge request exports types for Shoelace events that have event details. Types are not exported for events without any details since
CustomEvent
or justEvent
can be used for those events.This removes the need for consumers using TypeScript to redefine the events, and ensures that any breaking changes in Shoelace events will be detected at transpile time by consumers.
I didn't add a definition for
sl-error
here because it has no details in some contexts (eg.sl-icon
), but does have details when fired fromsl-include
, so I wasn't sure what you wanted done for this case. All other events with details should be covered.