#n10000k
1 messages · Page 1 of 1 (latest)
Hi there!
If you use a recent version of stripe-node it should automatically include all types.
Which version are you using?
Have you read this? https://github.com/stripe/stripe-node#usage-with-typescript
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?
I'm slightly lost. if you're using the types, they do have definitions for all these things , e.g. https://github.com/stripe/stripe-node/blob/master/types/Customers.d.ts#L153-L165
what kind of errors are you getting when you don't use ts-ignore exactly?
I don't have a TS project setup for this right now but I'll try spin something up now
I'll list the errors give me a sec
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)
and if you do const stripeCustomer: Customer = await stripe.customers.retrieve(..) ?
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',
],
});
?
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.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
yes. It's also in the example my colleague linked you earlier https://github.com/stripe/stripe-node#usage-with-typescript
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',
],
});
let me look. My guess is you're not using our type definitions and using something else, but looking
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
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
@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;
}
that code has to be inside the if statement where you determine it's not deleted.
I get the same issue:
if (stripeCustomer.deleted !== true) {
if (!stripeCustomer.invoice_settings as Stripe.Customer.InvoiceSettings) {
return null;
}
}
full snippet:
yeah but that's because you're doing a boolean comparison in a way that Typescript doesn't like.
for example if ((stripeCustomer.invoice_settings as Stripe.Customer.InvoiceSettings) !== null) { works.
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
yes
Stripe.Customer | Stripe.DeletedCustomer is from the retrieve anyway so don't really need that
ok cool, appreciate your help 🙌
happy to help