【发布时间】:2021-04-14 16:31:01
【问题描述】:
我正在尝试将输入参数正确键入到条带的createPaymentMethod 方法中,默认值为“card”,如下所示
const createStripePaymentMethod = async (paymentType: string = "card", cardElement: StripeCardElement) => {
... do some things
let response = await stripe.createPaymentMethod({
type: paymentType,
card: cardElement,
});
我将paymentType 输入为字符串,但这样做会给我一个类型错误:
Type 'String' is not assignable to type '"card" | "alipay" | "au_becs_debit" | "bancontact" | "eps" | "giropay" | "ideal" | "p24" | "fpx" | "sepa_debit" | "sofort"'.
Type 'String' is not assignable to type '"sofort"'.ts(2322)
payment-intents.d.ts(38, 5): The expected type comes from property 'type' which is declared here on type 'CreatePaymentMethodData'
编译器似乎表明正确的类型是CreatePaymentMethodData,但如果我用string 代替CreatePaymentMethodData,我会得到另一个错误:
Type 'string' is not assignable to type 'CreatePaymentMethodData'.ts(2322)
查看 payment-intents.d.ts,我怀疑这实际上是我需要使用的类型;原来的 typeerror 似乎表明有一个我需要引用的枚举,但我无处可寻 - 我该如何正确输入?
任何帮助将不胜感激。
tsconfig:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"types": ["googlemaps", "jest"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src"]
}
【问题讨论】:
标签: typescript stripe-payments typescript-typings