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: typescript autocomplete and types for public api #292

Merged
merged 8 commits into from
Oct 6, 2020

Conversation

ankeetmaini
Copy link
Contributor

@ankeetmaini ankeetmaini commented Oct 3, 2020

It also uses the approach defined in #250 to use interface types instead of a big conditional.

Fixes #250
Fixes #279

single event
Screenshot 2020-10-03 at 8 42 44 PM

multiple events
Screenshot 2020-10-03 at 8 44 57 PM

event names
Screenshot 2020-10-03 at 8 43 09 PM

@gr2m gr2m added Type: Bug Something isn't working as documented, or is being fixed typescript Relevant to TypeScript users only labels Oct 4, 2020
Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

thanks for getting this started!

| WatchEvent
| WorkflowDispatchEvent
| WorkflowRunEvent;
type All = keyof EventTypesPayload;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we get rid of this file altogether?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! I only did not remove it as it was exported from entrypoint. Will this be a breaking change for the users?


class Webhooks<T extends WebhookEvent = WebhookEvent> {
class Webhooks<T extends WebhookEvent = WebhookEvent, U = {}> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly is the U here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

U represents the additional properties that the transform function returns apart from the event payload object. Used here https://github.com/ankeetmaini/webhooks.js/blob/4e8d65b891f4adaa1426eb505a92b321c307bb2f/test/typescript-validate.ts#L124

Copy link
Contributor

Choose a reason for hiding this comment

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

I think before the type was inferred automatically from the return type of the transform method, is that still the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how that can be achieved. I can try and move the Option<T> param up so that it can be passed down to on handler and then try and extract the return type of Option<T>['transform'] but since the transform function would return an intersection type like WebhookEvent<any> & {some: 'other', type: 'attributes'} I can't use a conditional value to extract the second part. Mostly because intersection types are not distributed like union ones.

Also if I take the entire value, then intellisense goes out the window as WebhookEvent<any> takes precedence.

Copy link
Contributor Author

@ankeetmaini ankeetmaini Oct 5, 2020

Choose a reason for hiding this comment

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

This can probably be solved by changing the Webhook<T> signature, right now T is the actual payload, if we're to map that as the event name, then we can also theoretically get the value returned automatically. But this would be a big change like I was trying to attempt in #258 and I was all over the place :P

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, let's leave it as is, we can do a follow up PR if we come up with something clever :)

Can you please add a comment explaining the U type parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added the comment! I'll try and see if I can improve the types further in a follow up PR :D

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Thanks for getting this started!

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Changes look good to me, and I confirmed locally that #279 is resolved by this PR

@dominguezcelada @wolfy1339 can one of you have another look?

@wolfy1339
Copy link
Member

Seems to work fine for me

@gr2m gr2m merged commit 22e9ff9 into octokit:master Oct 6, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2020

🎉 This PR is included in version 7.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@oscard0m
Copy link
Member

oscard0m commented Oct 8, 2020

Changes look good to me, and I confirmed locally that #279 is resolved by this PR

@dominguezcelada @wolfy1339 can one of you have another look?

Sorry for the delay on following up this. Even it's already merged. Looks good to me 👍🏽

@ankeetmaini amazing effort and job here. Congrats! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented, or is being fixed typescript Relevant to TypeScript users only
Projects
None yet
4 participants