#n10000k

1 messages · Page 1 of 1 (latest)

tired gustBOT
dire arch
#

Hi there!

#

If you use a recent version of stripe-node it should automatically include all types.

#

Which version are you using?

brazen delta
#

latest version

#

13.3

dire arch
brazen delta
#

yes, but our business don't allow for use of ts-ignore comments, the package doesn't verify the explaind field so I have to manually define the types for all things like this?

#

this isn't possible either - stripeCustomer.invoice_settings.default_payment_method as Stripe.Customer.InvoiceSettings

#

even without default_payment_method the types don't match up?

zinc marlin
brazen delta
#

I'll list the errors give me a sec

tired gustBOT
brazen delta
#

Ok so debug wise;

  • package.json - "stripe": "^13.3.0",
    • restarted ts server locally

Code wise:

    const stripeCustomer = await this.stripe.customers.retrieve(subscription.providerCustomerId, {
      expand: [
        'invoice_settings.default_payment_method',
      ],
    });

This fetches the customer + expands to get the invoice_settings default payment method

Then I do:

    if (!stripeCustomer.invoice_settings) {
      return null;
    }

Gives me:

Property 'invoice_settings' does not exist on type 'Response<Customer | DeletedCustomer>'.
  Property 'invoice_settings' does not exist on type 'DeletedCustomer & { lastResponse: { headers: { [key: string]: string; }; requestId: string; statusCode: number; apiVersion?: string; idempotencyKey?: string; stripeAccount?: string; }; }'.ts(2339)
#

If I do:

    if (!stripeCustomer.invoice_settings.default_payment_method as Stripe.Customer.InvoiceSettings) {
      return null;
    }

I get:

Conversion of type 'boolean' to type 'InvoiceSettings' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)
zinc marlin
#

and if you do const stripeCustomer: Customer = await stripe.customers.retrieve(..) ?

brazen delta
#

Same with just calling stripeCustomer.invoice_settings I get:

Conversion of type 'boolean' to type 'InvoiceSettings' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)
interface Stripe.Customer.InvoiceSettings
namespace Stripe.Customer.InvoiceSettings
#

will try

#

like this?

    const stripeCustomer: Stripe.Customer = await this.stripe.customers.retrieve(subscription.providerCustomerId, {
      expand: [
        'invoice_settings.default_payment_method',
      ],
    });
#

?

zinc marlin
#

the error tells you that the return type can be a Customer or a Deleted Customer (https://stripe.com/docs/api/customers/retrieve " If it’s for a deleted Customer, a subset of the customer’s information is returned, including a deleted property that’s set to true.") so you need to tell the code what concrete type it is.

zinc marlin
brazen delta
#

Yeah so I set it to Stripe.Customer so it doesn't use the Stripe.DeletedCustomer and I get the same issue

#

ts error:

Type 'Response<Customer | DeletedCustomer>' is not assignable to type 'Customer'.
  Type 'DeletedCustomer & { lastResponse: { headers: { [key: string]: string; }; requestId: string; statusCode: number; apiVersion?: string; idempotencyKey?: string; stripeAccount?: string; }; }' is missing the following properties from type 'Customer': balance, created, default_source, description, and 5 more.ts(2322)
#

Is Response exported in the SDK anywhere so I can use that to wrap?

#

Example used for the above error:

    const stripeCustomer: Stripe.Customer = await this.stripe.customers.retrieve(subscription.providerCustomerId, {
      expand: [
        'invoice_settings.default_payment_method',
      ],
    });
zinc marlin
#

let me look. My guess is you're not using our type definitions and using something else, but looking

brazen delta
#

The type from the Stripe sdk -

#

exported from line 154 namespace Customer{ in types from the sdk

#

Customers.d.ts

#

(I'll be back in 30, getting some lunch) thanks for looking into this @zinc marlin

zinc marlin
#

the example in the repo README is not the same because the only outcome of creating a customer is a Customer, but when retrieving a Customer as in your code you can get a Customer or DeletedCustomer

#

I set up a project and tried this on my end and it works

brazen delta
#

@zinc marlin that typing is fine but still gives me:

Property 'invoice_settings' does not exist on type 'Customer | DeletedCustomer'.
Property 'invoice_settings' does not exist on type 'DeletedCustomer'.ts(2339)
#

on:

    if (!stripeCustomer.invoice_settings as Stripe.Customer.InvoiceSettings) {
      return null;
    }
zinc marlin
#

that code has to be inside the if statement where you determine it's not deleted.

brazen delta
#

I get the same issue:

    if (stripeCustomer.deleted !== true) {
      if (!stripeCustomer.invoice_settings as Stripe.Customer.InvoiceSettings) {
        return null;
      }
    }
#

full snippet:

zinc marlin
#

yeah but that's because you're doing a boolean comparison in a way that Typescript doesn't like.

brazen delta
#

oh

#

i remove the type yeah

zinc marlin
#

for example if ((stripeCustomer.invoice_settings as Stripe.Customer.InvoiceSettings) !== null) { works.

brazen delta
#
    if (stripeCustomer.deleted !== true) {
      if (!stripeCustomer.invoice_settings.default_payment_method) {
        return null;
      }
    }

This works

#

ok great, thanks for that, so tldr wrap it in a deleted check

zinc marlin
#

yes

brazen delta
#

Stripe.Customer | Stripe.DeletedCustomer is from the retrieve anyway so don't really need that

#

ok cool, appreciate your help 🙌

zinc marlin
#

happy to help