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

Improve typescript support #1291

Merged
merged 1 commit into from
Nov 11, 2021

Conversation

jnraine-stripe
Copy link
Contributor

@jnraine-stripe jnraine-stripe commented Nov 11, 2021

Notify

r? @richardm-stripe

Summary

This improves three friction points when working with Typescript:

  1. new StripeResource(stripe) now works. Prior to this change, attempting to construct StripeResource with its one required argument would fail a type check. Support for a constructor was added in Fix StripeResource.extend type #1245 and extended in Fix StripeResource constructor type #1249 (never merged). Thanks @rattrayalex!
  2. StripeResource.extend now returns a class that is properly typed when instantiated. Prior to this change, you could extend StripeResource but your extensions wouldn't appear on the object after construction. Now, it does.
  3. StripeResource.method returns a function that returns Response<object>. Prior to this change, extensions created with StripeResource.method would have a return type of object. Returning Response<object> makes it possible to call functions like lastResponse without casting the value.
  4. StripeResource.method now accepts generic type that sets the response object's type. For example, if you're hitting a customer endpoint, you can do this: StripeResource.method<Customer>(...). When you use that extension, it will return Response<Customer>. This eliminates the need to cast when calling StripeResource extension methods.

See changes to typescriptTest.ts for these updates in action.


fixes #1249

This improves three friction points when working with Typescript:

1. `new StripeResource(stripe)` now works. Prior to this change, attempting to construct `StripeResource` with its one required argument would fail a type check. Support for a constructor was added in stripe#1245 and extended in stripe#1249 (never merged). Thanks @rattrayalex!
2. `StripeResource.extend` now returns a class that is properly typed when instantiated. Prior to this change, you could extend `StripeResource` but your extensions wouldn't appear on the object after construction. Now, it does.
3. `StripeResource.method` returns a function that returns `Response<object>`. Prior to this change, extensions created with `StripeResource.method` would have a return type of `object`. Returning `Response<object>` makes it possible to call functions like `lastResponse` without casting the value.
4. `StripeResource.method` now accepts generic type that sets the response object's type. For example, if you're hitting a customer endpoint, you can do this: `StripeResource.method<Customer>(...)`. When you use that extension, it will return `Response<Customer>`. This eliminates the need to cast when calling StripeResource extension methods.

# Conflicts:
#	types/lib.d.ts
#	types/test/typescriptTest.ts
@CLAassistant
Copy link

CLAassistant commented Nov 11, 2021

CLA assistant check
All committers have signed the CLA.

@richardm-stripe
Copy link
Contributor

This looks good to me!

@richardm-stripe richardm-stripe merged commit c330f9d into stripe:master Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants