You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to add a feature that generated a receipt, based on the stripe payment information, and ran into some issues with the type interface.
The most significant difficulty in my cases was the need to use the as unknown astechnique with an override type, when consuming receipt data — primarily due to a pluralization inconsistency between terminalVerificationResult (expected) and terminalVerificationResults (actual)
Big fan of the decision to prefer verbose property names over abbreviation, but I think this is an area where type annotations could be really useful, as they could include the standard abbreviation for a given field.
Eventually I landed on this (although I think I need to iron out a couple things)
I think there's a way to include type narrowing via the return type, but it may also be advantageous to have a generic where users can pass the intended permutation as an argument to the type interface.
Consolidation?
I think there's also something to be said for consolidating fields, eg:
type CardPresentDetails = CardPresentDetailsStandard | InteracPresentDetails
It was sometimes difficult to know which selection path to use — and it would be great to be able to rely more decisively on the type signature and remove the need for trial and error.
Please let me know if I can be of any additional help!
Thanks!!
The text was updated successfully, but these errors were encountered:
leggomuhgreggo
changed the title
Fix PaymentIntent type inconsistencies
[bug] PaymentIntent type inconsistencies
Nov 20, 2024
Overview
I needed to add a feature that generated a receipt, based on the stripe payment information, and ran into some issues with the type interface.
The most significant difficulty in my cases was the need to use the
as unknown as
technique with an override type, when consuming receipt data — primarily due to a pluralization inconsistency betweenterminalVerificationResult
(expected) andterminalVerificationResults
(actual)Example Code
Upon investigating further, there appear to be additional type mismatch issues (see below)
Repro
console.log(JSON.stringify(paymentIntent))
in payment handlerconst ExamplePaymentIntent: PaymentIntent.Type = {...}
Screenshot
Additional Considerations
There's a lot one could do with type definitions
I think to start with, just ensuring that the types match what's being generated would be an excellent improvement.
Type Annotations?
Another great enhancement would be to add annotations to the types so that they can be picked up via intellisense.
Here's an example where it could be useful:
Our existing receipt interface included an
emv
object and it wasn't immediately obvious how to map these fieldsBig fan of the decision to prefer verbose property names over abbreviation, but I think this is an area where type annotations could be really useful, as they could include the standard abbreviation for a given field.
Eventually I landed on this (although I think I need to iron out a couple things)
Discriminated Unions?
I think in a perfect world the PaymentIntent type would be a union of every major permutation of the model, something like:
type PaymentIntent = PaymentIntentInitial | PaymentIntentConfirmed | PaymentIntentCancelled
Note: Since it's aggregating other models the unions, those nested, constituent models would also probably have union types
Ideally with this approach there's a dedicated enum field that can be used for the discriminated union pattern
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
I think there's a way to include type narrowing via the return type, but it may also be advantageous to have a generic where users can pass the intended permutation as an argument to the type interface.
Consolidation?
I think there's also something to be said for consolidating fields, eg:
type CardPresentDetails = CardPresentDetailsStandard | InteracPresentDetails
It was sometimes difficult to know which selection path to use — and it would be great to be able to rely more decisively on the type signature and remove the need for trial and error.
Please let me know if I can be of any additional help!
Thanks!!
The text was updated successfully, but these errors were encountered: