stripe / stripe-js

Loading wrapper for Stripe.js
https://stripe.com/docs/js
MIT License
632 stars 156 forks source link

[BUG]: confirmSetup overloads are incorrectly throwing errors #673

Open TheDutchCoder opened 3 weeks ago

TheDutchCoder commented 3 weeks ago

What happened?

When using confirmSetup in TypeScript, with, for example, options like these, TS will throw errors for missing properties like clientSecret, even though this is only required in a few of the overloads. It seems to have trouble selecting the proper one.

const stripeArguments = {
  redirect: 'if_required',
  elements: (stripeElements.value as StripeElements),
  confirmParams: {
    return_url: returnUrl,
  },
}

// Should not throw an error.
stripe?.confirmSetup(stripeArguments)

Additionally, the type for redirect is set to "always", which is one of the options, but doesn't include "if_required". it looks like it just uses the last overload at all times, which has this signature:

confirmSetup(options: {
  elements?: StripeElements;
  clientSecret: string;
  confirmParams: setupIntents.ConfirmSetupData;
  redirect?: 'always';
}): Promise<never | {error: StripeError}>;

The only workaround I have found is by creating a custom type to force the right overload to be selected:

type OverloadOptions = {
  elements: StripeElements
  confirmParams?: Partial<ConfirmSetupData>
  redirect: 'if_required'
}

const stripeArguments: OverloadOptions = {
  redirect: 'if_required',
  elements: (stripeElements.value as StripeElements),
  confirmParams: {
    return_url: returnUrl,
  },
}

Environment

Chrome 130 on MacOS 15.0.1

Reproduction

No response

brendanm-stripe commented 2 weeks ago

The params you shared should match the type defined here and used in this test:

confirmSetup(options: {
    elements: StripeElements;
    confirmParams?: Partial<setupIntents.ConfirmSetupData>;
    redirect: 'if_required';
  }): Promise<SetupIntentResult>;

but may require you to type the params explicitly when passed as an object due to type inference.