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

Add event types #1921

Merged
merged 2 commits into from
Oct 12, 2023
Merged

Add event types #1921

merged 2 commits into from
Oct 12, 2023

Conversation

pakrym-stripe
Copy link
Contributor

@pakrym-stripe pakrym-stripe commented Oct 10, 2023

Renames the current Event type to EventBase. Adds generated event definitions for known events with correct data.object reference type.

Defines a new Event type that's a union of all strongly-typed events. The Event union is equivalent in structure to the old Event type except the type property that becomes an enum of all known even types and not just a string.

const event = await stripe.webhooks.constructEventAsync(body, signature, secret);

// Allows narrowing of event type
if (event.type == 'customer.created') {
  const customer = event.data.object; // customer is typed as Stripe.Customer
  console.log(customer.email); // No error, typed property access
}

// TypeScript error
// This comparison appears to be unintentional because the types "account.application.authorized" | ... and  "customer.crated"  have no overlap.
if (event.type == 'customer.crated') {}

Copy link
Contributor

@anniel-stripe anniel-stripe left a comment

Choose a reason for hiding this comment

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

🔥🔥🔥

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.

2 participants