Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incomplete Typescript Types #1999

Closed
sakgoyal opened this issue Jan 20, 2024 · 3 comments
Closed

Incomplete Typescript Types #1999

sakgoyal opened this issue Jan 20, 2024 · 3 comments
Labels

Comments

@sakgoyal
Copy link

sakgoyal commented Jan 20, 2024

Describe the bug

The types for retrieving are incomplete.
(checkout.sessions.retrieve, some.function.retrieve, ...etc)

the return type gives customers as optionals when it needs to depend on the options passed in as params. if the customer section is not included in the expand param, the return type for customer should not be "string | stripe.customer". it should just be string. the same thing for all the other extendable keys.

it makes it very difficult to use typescript without typecasting which can cause problems if I do them incorrectly.

Can the types be fixed to give correct types?

here, since 'subscription' is expanded, the type should be stripe.subscription | null, not string | stripe.subscription | null
image

  • OS: Ubuntu
  • Node version 20
  • Library version 14.11
  • API version 2023-10-16
@sakgoyal sakgoyal added the bug label Jan 20, 2024
@sakgoyal
Copy link
Author

sakgoyal commented Jan 20, 2024

Here is some typescript I was playing around with to provide an example on how this can be achieved

type StripeCustomer = { // this type already exists
  email: string;
  id: number;
  // ...
};

type CheckoutSession = { // this type already exists
  customer: string | StripeCustomer;
  // ...
};

type ValidKeys = keyof CheckoutSession;

function retrieve(options?: { expand?: ValidKeys[] }): CheckoutSession { // fix this function to return correctly
  if (options?.expand && options.expand.includes('customer')) {
    return { customer: { email: "foo@example.com", id: 1234 } };
  } else {
    return { customer: "default string value" };    
  }
}

// Examples
const objWithStringCustomer = retrieve(); // { customer: "default string value" }
const objWithObjectCustomer = retrieve({ expand: ['customer'] }); // { customer: { email: "foo@example.com", id:1234 } }

@pakrym-stripe
Copy link
Contributor

Duplicate of #1556.

In short, we tried to prototype what full typing support would look like, and it led to extreme complications of the SDK types.

@EddyVinck
Copy link

Just chiming in, this is the solution I came up with for an expanded product on a price:

if (typeof price.product === 'object' && !price.product?.deleted) {
  // access price.product safely
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants