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

[Feature] Strongly Typed Event Type for Stripe.Event #902

Closed
Ry-DS opened this issue May 25, 2020 · 11 comments
Closed

[Feature] Strongly Typed Event Type for Stripe.Event #902

Ry-DS opened this issue May 25, 2020 · 11 comments
Assignees
Labels
future type definition Marks issue related to outdated or incorrect TypeScript type definition

Comments

@Ry-DS
Copy link

Ry-DS commented May 25, 2020

Not too important, but it would be nice if this type:

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.

@Ry-DS Ry-DS changed the title [Feature] Stronly Typed Event Type for Stripe.Event [Feature] Strongly Typed Event Type for Stripe.Event May 25, 2020
@richardm-stripe richardm-stripe self-assigned this Jun 1, 2020
@richardm-stripe
Copy link
Contributor

richardm-stripe commented Jun 1, 2020

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.

@JustinTRoss
Copy link

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.

@JustinTRoss
Copy link

This seems to also be referenced by #758

@svallory
Copy link

svallory commented Aug 11, 2020

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.

@franky47
Copy link

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

@kamil-stripe kamil-stripe added the type definition Marks issue related to outdated or incorrect TypeScript type definition label Apr 20, 2022
@saiichihashimoto
Copy link

Strongly related: #1387

@jameshfisher
Copy link

Duplicate of #758?

@StevenLangbroek
Copy link

Someone put in the effort of making a disjoint union of it and publishing it to npm: https://npm.im/stripe-event-types

@huw
Copy link

huw commented Sep 10, 2023

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).

@huw
Copy link

huw commented Oct 16, 2023

I think #1921 (v14.0.0) solves this issue!

@pakrym-stripe
Copy link
Contributor

This is now available starting with v13.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future type definition Marks issue related to outdated or incorrect TypeScript type definition
Projects
None yet
Development

No branches or pull requests