Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stripe.customers.retrieve causing issues as it is now returning Stripe.Customer | Stripe.DeletedCustomer

I am upgrading to the latest Stripe API version (2020-03-02) and I am not sure how to access the values on my customer object as it now just shows the union of the properties between Stripe.Customer and Stripe.DeletedCustomer.

Any tips on how to check the type and convert to a Stripe.Customer type?

I am getting the following error:

Property 'metadata' does not exist on type 'Customer | DeletedCustomer'. Property 'metadata' does not exist on type 'DeletedCustomer'.

 const customer: Stripe.Customer | Stripe.DeletedCustomer = await stripe.customers.retrieve(customerId);
 const uid = customer.metadata.firebaseUID;

Note that Customer and DeletedCustomer are interfaces:

namespace Stripe {
     interface DeletedCustomer {
          id: string;
          object: 'customer';
          deleted: true;
        }
    
     interface Customer {
          id: string;
          metadata: Metadata;
    ...
    }
}
like image 545
MadMac Avatar asked Jun 25 '26 05:06

MadMac


2 Answers

I found this comment for discriminating between Stripe.Customer and Stripe.DeletedCustomer.

So this worked for me:

if (customer.deleted === true) {
  // TypeScript treats customer as Stripe.DeletedCustomer
} else {
  // TypeScript treats customer as Stripe.Customer
}

I originally tried just if (customer.deleted), but that didn't work. TypeScript only discriminated after I added the === true.

like image 120
Danny Guo Avatar answered Jun 27 '26 00:06

Danny Guo


Stripe provides a common deleted flag that you can use to determine if you have a Customer or DeletedCustomer.

// You don't need the explicit type, it's inferred
const customer = await stripe.customers.retrieve(customerId);
if (!customer.deleted) {
   const uid = customer.metadata.firebaseUID;
   ...
}

See this GitHub issue for more information.

like image 45
cdmckay Avatar answered Jun 27 '26 01:06

cdmckay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!