What is the best way to check which object is returned from an API in typescript?

MadMac

Stripe can return 3 different object types from as the customer. I want the customer id which is what is normally returned.

What I have below is pretty ugly.

Is there a better way to do this?

  const stripeCustomer: string | Stripe.Customer | Stripe.DeletedCustomer = checkoutSession.customer;
  let stripeCustomerId: string;

  if (stripeCustomer instanceof Object && "email" in stripeCustomer) {
    // customer is of type Stripe.Customer
      stripeCustomerId = stripeCustomer.id;
  } else if (typeof stripeCustomer === 'string') { 
    // customer is id
    stripeCustomerId = stripeCustomer;
  } else {
    // customer is of type Stripe.DeletedCustomer
    stripeCustomerId = null;
  }
Alexander

You can use a user-defined type guards, which let your plug your own custom type-checking logic into the type system.

When they're true for an object, they inform the flow-sensitive typing that the object has the specified type:

function isStripeCustomer(object: any): object is Stripe.Customer {
    return object instanceof Object 
       && "object" in object 
       && object.object === 'customer' 
       && !object.deleted
}

function isStripCustomerID(object: any): object is string {
    return typeof object === "string"
}

function isStripeDeletedCustomer(object: any): object is Stripe.DeletedCustomer {
    return object instanceof Object
       && "object" in object 
       && object.object === 'customer' 
       && object.deleted
}

const customer: string | Stripe.Customer | Stripe.DeletedCustomer = checkoutSession.customer;

let stripeCustomerId: string | null

if (isStripeCustomer(customer)) {
    stripeCustomerId = customer.id
} else if (isStripCustomerID(customer)) { 
    stripeCustomerId = customer;
} else if (isStripeDeletedCustomer(customer) {
    stripeCustomerId = null;
} else {
    // It was none of the known types. Perhaps a new type of result was
    // add in a future version of their API?

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is best way for object null and empty check?

What is the best way to check of an object is an Enum in Cython?

Best way to check and assign to object from response?

Junit: What is the best way to check which test is testing a function in Intellij?

which is the best way to define an object in typescript and access it values?

What would be the best way to check if any of the keys in an array of objects contain a value from an object of arrays array?

PHP - What is the best way to access a property of an object which is an array element

what is the best way to setState which contains nested Object

What is the best way to check if a list of object has changed?

What is the best way to check for the object with the most keys in a list?

what's the best way to check if one object is a prototype of another?

What is the best way to Null check input fields in the request object in Java?

What is the best way to create an object out of interface in typescript

Which is best way to analyse this object

What is the best way to retrieve any object from Mongo using Spring?

What is the best way to add options to a select from a JavaScript object with jQuery?

What is the best way to add options to a select from as a JavaScript object with jQuery?

What's the best way to get entities from a object?

What's the best way to find an object from a string in kotlin?

What is the best way to make a detail page from an object list?

What is the best way extract a key and value from Axios response object?

Which is the best way to check for the existence of an attribute?

Which is the best way to check for the existence of an attribute?

Which is the best way to check return result?

What is the best way to empty an object?

What is the best way to check if there are 4 items vertically horizontaly or diagonaly which end the game of connect 4

What is the best way to get the first letter from a string in Java, returned as a string of length 1?

What is the best way to work with data from a backend api in an angular service?

What's the best way to interpret JSON from an api ?