Node.js stripe.customers.retrieve导致问题,因为它现在返回stripe.Customer | stripe.DeletedCustomer
我正在升级到最新的Stripe API版本(2020-03-02),我不确定如何访问我的customer对象上的值,因为它现在只显示Stripe.customer和Stripe.DeletedCustomer之间属性的联合 关于如何检查类型并转换为条带的任何提示。客户类型 我得到以下错误: 类型“Customer”上不存在属性“metadata”| 删除“客户”。类型上不存在属性“元数据” “删除客户” 请注意,Customer和DeletedCustomer是接口:Node.js stripe.customers.retrieve导致问题,因为它现在返回stripe.Customer | stripe.DeletedCustomer,node.js,typescript,stripe-payments,Node.js,Typescript,Stripe Payments,我正在升级到最新的Stripe API版本(2020-03-02),我不确定如何访问我的customer对象上的值,因为它现在只显示Stripe.customer和Stripe.DeletedCustomer之间属性的联合 关于如何检查类型并转换为条带的任何提示。客户类型 我得到以下错误: 类型“Customer”上不存在属性“metadata”| 删除“客户”。类型上不存在属性“元数据” “删除客户” 请注意,Customer和DeletedCustomer是接口: namespace Str
namespace Stripe {
interface DeletedCustomer {
id: string;
object: 'customer';
deleted: true;
}
interface Customer {
id: string;
metadata: Metadata;
...
}
}
要区分类型,您可以:
条带.DeletedCustomer
没有元数据属性,因此您可以简单地验证此属性是否存在,如果存在,则“customer”类型为“Stripe.customer”,否则类型为“Stripe.DeletedCustomer”:
const customer:Stripe.customer | Stripe.DeletedCustomer=wait Stripe.customers.retrieve(customerId);
if(客户中对象和“元数据”的客户实例){
//$customer的类型为“Stripe.customer”
const uid=customer.metadata.firebaseUID;
}
否则{
//$customer的类型为“Stripe.DeletedCustomer”
}
@请参阅:为什么不检查customer是否为条带。在运行访问元数据的行之前,请先检查customer?@ddcastrod我该怎么做?我尝试了typeof和instanceof,但没有成功。对typescript来说还是比较新的。实际上,我只是在该行之前使用了以下代码,它现在不会抛出错误并编译:if(customer.deleted){//customer is deleted//TODO:Throw error return;}我无法让它工作。我还尝试了以下操作,但没有成功。const subscription:Stripe.subscription | string=wait Stripe.subscriptions.retrieve(checkoutSession.subscription,{expand:['latest\u invoice'],stripeAccount:
${connectedStripeAccountId}
,});let invoice:Stripe.invoice;//if(订阅中对象的订阅实例和“最新发票”){if((订阅类型)=='string'){return;}else invoice=subscription.latest\u invoice;}我得到错误““字符串”类型上不存在属性“最新发票”
namespace Stripe {
interface DeletedCustomer {
id: string;
object: 'customer';
deleted: true;
}
interface Customer {
id: string;
metadata: Metadata;
...
}
}