1
1
import type {
2
2
Stripe ,
3
+ StripeAddressElement ,
3
4
StripeAddressElementOptions ,
5
+ StripeAffirmMessageElement ,
4
6
StripeAffirmMessageElementOptions ,
7
+ StripeAfterpayClearpayMessageElement ,
5
8
StripeAfterpayClearpayMessageElementOptions ,
9
+ StripeAuBankAccountElement ,
6
10
StripeAuBankAccountElementOptions ,
11
+ StripeCardCvcElement ,
7
12
StripeCardCvcElementOptions ,
13
+ StripeCardElement ,
8
14
StripeCardElementOptions ,
15
+ StripeCardExpiryElement ,
9
16
StripeCardExpiryElementOptions ,
17
+ StripeCardNumberElement ,
10
18
StripeCardNumberElementOptions ,
11
19
StripeConstructorOptions ,
20
+ StripeCurrencySelectorElement ,
12
21
StripeElementType ,
13
22
StripeElements ,
14
23
StripeElementsOptions ,
15
24
StripeElementsOptionsClientSecret ,
16
25
StripeElementsOptionsMode ,
26
+ StripeEpsBankElement ,
17
27
StripeEpsBankElementOptions ,
28
+ StripeExpressCheckoutElement ,
18
29
StripeExpressCheckoutElementOptions ,
30
+ StripeFpxBankElement ,
19
31
StripeFpxBankElementOptions ,
32
+ StripeIbanElement ,
20
33
StripeIbanElementOptions ,
34
+ StripeIdealBankElement ,
21
35
StripeIdealBankElementOptions ,
36
+ StripeIssuingCardCopyButtonElement ,
22
37
StripeIssuingCardCopyButtonElementOptions ,
38
+ StripeIssuingCardCvcDisplayElement ,
23
39
StripeIssuingCardCvcDisplayElementOptions ,
40
+ StripeIssuingCardExpiryDisplayElement ,
24
41
StripeIssuingCardExpiryDisplayElementOptions ,
42
+ StripeIssuingCardNumberDisplayElement ,
25
43
StripeIssuingCardNumberDisplayElementOptions ,
44
+ StripeIssuingCardPinDisplayElement ,
26
45
StripeIssuingCardPinDisplayElementOptions ,
46
+ StripeLinkAuthenticationElement ,
47
+ StripeP24BankElement ,
27
48
StripeP24BankElementOptions ,
49
+ StripePaymentElement ,
28
50
StripePaymentElementOptions ,
51
+ StripePaymentMethodMessagingElement ,
29
52
StripePaymentMethodMessagingElementOptions ,
53
+ StripePaymentRequestButtonElement ,
30
54
StripePaymentRequestButtonElementOptions ,
55
+ StripeShippingAddressElement ,
31
56
StripeShippingAddressElementOptions ,
32
57
} from "@stripe/stripe-js"
33
58
@@ -65,7 +90,7 @@ export type StripeElementOptionsMap = {
65
90
cardCvc : StripeCardCvcElementOptions
66
91
cardExpiry : StripeCardExpiryElementOptions
67
92
cardNumber : StripeCardNumberElementOptions
68
- currencySelector : undefined
93
+ currencySelector ? : undefined
69
94
epsBank : StripeEpsBankElementOptions
70
95
expressCheckout : StripeExpressCheckoutElementOptions
71
96
fpxBank : StripeFpxBankElementOptions
@@ -76,14 +101,42 @@ export type StripeElementOptionsMap = {
76
101
issuingCardExpiryDisplay : StripeIssuingCardExpiryDisplayElementOptions
77
102
issuingCardNumberDisplay : StripeIssuingCardNumberDisplayElementOptions
78
103
issuingCardPinDisplay : StripeIssuingCardPinDisplayElementOptions
79
- linkAuthentication : undefined
104
+ linkAuthentication ? : undefined
80
105
p24Bank : StripeP24BankElementOptions
81
106
payment : StripePaymentElementOptions
82
107
paymentMethodMessaging : StripePaymentMethodMessagingElementOptions
83
108
paymentRequestButton : StripePaymentRequestButtonElementOptions
84
109
shippingAddress : StripeShippingAddressElementOptions
85
110
}
86
111
112
+ export type StripeElementMap = {
113
+ address : StripeAddressElement
114
+ affirmMessage : StripeAffirmMessageElement
115
+ afterpayClearpayMessage : StripeAfterpayClearpayMessageElement
116
+ auBankAccount : StripeAuBankAccountElement
117
+ card : StripeCardElement
118
+ cardCvc : StripeCardCvcElement
119
+ cardExpiry : StripeCardExpiryElement
120
+ cardNumber : StripeCardNumberElement
121
+ currencySelector : StripeCurrencySelectorElement
122
+ epsBank : StripeEpsBankElement
123
+ expressCheckout : StripeExpressCheckoutElement
124
+ fpxBank : StripeFpxBankElement
125
+ iban : StripeIbanElement
126
+ idealBank : StripeIdealBankElement
127
+ issuingCardCopyButton : StripeIssuingCardCopyButtonElement
128
+ issuingCardCvcDisplay : StripeIssuingCardCvcDisplayElement
129
+ issuingCardExpiryDisplay : StripeIssuingCardExpiryDisplayElement
130
+ issuingCardNumberDisplay : StripeIssuingCardNumberDisplayElement
131
+ issuingCardPinDisplay : StripeIssuingCardPinDisplayElement
132
+ linkAuthentication : StripeLinkAuthenticationElement
133
+ p24Bank : StripeP24BankElement
134
+ payment : StripePaymentElement
135
+ paymentMethodMessaging : StripePaymentMethodMessagingElement
136
+ paymentRequestButton : StripePaymentRequestButtonElement
137
+ shippingAddress : StripeShippingAddressElement
138
+ }
139
+
87
140
export const ERRORS = {
88
141
STRIPE_NOT_LOADED :
89
142
"Stripe is not loaded. Include it as script or load using loadStripe method of @stripe/stripe-js" ,
@@ -127,10 +180,15 @@ export const createElements = (
127
180
}
128
181
}
129
182
130
- export const createElement = (
183
+ type CreateFn = < T extends StripeElementType > (
184
+ elementType : T ,
185
+ options ?: StripeElementOptionsMap [ T ] ,
186
+ ) => StripeElementMap [ T ]
187
+
188
+ export const createElement = < T extends StripeElementType > (
131
189
elements : StripeElements ,
132
- elementType : StripeElementType ,
133
- options ?: StripeElementOptionsMap [ StripeElementType ] ,
190
+ elementType : T ,
191
+ options ?: StripeElementOptionsMap [ T ] ,
134
192
) => {
135
193
try {
136
194
if ( ! elements ) {
@@ -139,7 +197,10 @@ export const createElement = (
139
197
if ( ! elementType ) {
140
198
throw new Error ( ERRORS . ELEMENT_TYPE_NOT_DEFINED )
141
199
}
142
- return elements . create ( elementType , options )
200
+ // Type assertion to bypass the overloads issue
201
+ const create : CreateFn = elements . create . bind ( elements ) as CreateFn
202
+ const element = create ( elementType , options )
203
+ return element
143
204
} catch ( error ) {
144
205
console . error ( error )
145
206
}
0 commit comments