stripe-node
stripe-node copied to clipboard
[Feature] Strongly Typed Event Type for Stripe.Event
Not too important, but it would be nice if this type:
https://github.com/stripe/stripe-node/blob/02da7dc2c5edae280aaf5e3566f3b7b383024371/types/2020-03-02/Events.d.ts#L52
is strongly typed with all possible events. This would help catch bugs within switch statements or other logic statements, while also providing a nice menu of events within your IDE.
If you are open to it, I don't mind making a quick script to take the webhook dropdown and convert it to a type definition for this variable in a PR.
Hi @SimplyBallistic, thanks for the suggestion! Strongly typing this field is certainly desirable, but to make this happen we need to modify the (closed-source-for-now) tool that generates the type definitions from the openapi spec. We would also need to make sure that the spec contains an authoritative list of all the event types, which I suspect it may not, but I will investigate.
I came here to suggest the same. It's worth noting, if a blocker for getting a typedef together for Event
and Event.type
is around making it exhaustive, it would likely be nearly as helpful for folks to have a non-exhaustive typedef with an index to allow for options not included.
This seems to also be referenced by #758
There's a quick workaround for this. Add this to a declaration or any ts file imported in your project:
declare module 'stripe' {
namespace Stripe {
interface TypedEventData<T> extends Stripe.Event.Data {
object: T;
previous_attributes?: Partial<T>;
}
interface TypedEvent<T = any> extends Stripe.Event {
data: TypedEventData<T>;
type: Exclude<Stripe.WebhookEndpointCreateParams.EnabledEvent, '*'>;
}
}
}
Then use Stripe.TypedEvent<Stripe.Subscription>
for example. This could easily be added to Stripe's official typing too.
This could go even further IMO: webhook event types (eg: customer.created
) have a known payload type (eg: Customer
).
It would indeed require some internal tooling change at Stripe to connect event.type to its correct event.data type, but this would make it impossible to introduce bugs when writing webhooks handlers (eg: type-casting a payload into a Subscription where the actual object passed in is a Customer).
I've got an example of this running in my SaaS, it required doing the mapping manually though: https://gist.github.com/franky47/5f06c1e8271de40e28864ecd8d95ef3c
Strongly related: #1387
Duplicate of https://github.com/stripe/stripe-node/issues/758?
Someone put in the effort of making a disjoint union of it and publishing it to npm: https://npm.im/stripe-event-types
So the issue as described in the original post is fixed—event.type
is a union of string literals in v13.4.0. However, I think either this issue should be modified or we should create a new issue for making Stripe.Event
a discriminated union over event.type
(i.e. what stripe-event-types
does).
I think #1921 (v14.0.0) solves this issue!
This is now available starting with v13.11.0