如何使用枚举在打字稿中键入
我对如何与TS一起使用枚举感到有些困惑。您可以将枚举用作价值而不是类型吗? 我尝试了许多不同的方法,但无法解决。 这是我的代码,但似乎并没有打字:
这是
//interface.ts
export enum EMadeFrom {
'stripe',
'manually',
'app',
'invoice.payment_succeeded',
'customer.subscription.updated.trial',
'customer.subscription.updated.active',
}
export interface IPayments {
modelID: string
amount: number
paymentMethod: string
paymentMethodType: string
productType: string
stripeCustomerId: string
stripePaymentStatus: string
stripeId: string
stripePriceId: Array<string>
stripeSubscriptionStartDate: string
stripeSubscriptionEndDate: string
sessionPhoto: ISessionPhoto | null
madeFrom: EMadeFrom
createdAt: Date
updatedAt: Date
}
界面
const CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL =
'customer.subscription.updated.trial'
try {
const payment = await modelModel.createPayment(
model.id,
stripeInvoice.total / 100,
stripeCustomer.invoice_settings.default_payment_method,
'card',
productIsBundle ? BUNDLE : SUBSCRIPTION,
subscriptionUpdated.customer,
subscriptionUpdated.status,
subscriptionUpdated.id,
subscriptionUpdated.plan.id,
stripeSubscriptionStartDate,
stripeSubscriptionEndDate,
sessionPhoto,
CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL,
)
。
const createPayment = (
modelId: IPayments['modelID'],
amount: IPayments['amount'],
paymentMethod: IPayments['paymentMethod'],
paymentMethodType: IPayments['paymentMethodType'],
productType: IPayments['productType'],
stripeCustomerId: IPayments['stripeCustomerId'],
stripePaymentStatus: IPayments['stripePaymentStatus'],
stripeId: IPayments['stripeId'],
stripePriceId: IPayments['stripePriceId'],
stripeSubscriptionStartDate: IPayments['stripeSubscriptionStartDate'],
stripeSubscriptionEndDate: IPayments['stripeSubscriptionEndDate'],
sessionPhoto: null | IPayments['sessionPhoto'],
madeFrom: IPayments['madeFrom'],
) =>
new Promise((resolve, reject) => {
...
i'm a bit confused on how to use Enums with TS. Can you use an enum as a value instead of a type?
i tried many different ways but i couldn't solve it.
Here's my code but it doesn't seem to typeCheck it:
This is the interface.ts
//interface.ts
export enum EMadeFrom {
'stripe',
'manually',
'app',
'invoice.payment_succeeded',
'customer.subscription.updated.trial',
'customer.subscription.updated.active',
}
export interface IPayments {
modelID: string
amount: number
paymentMethod: string
paymentMethodType: string
productType: string
stripeCustomerId: string
stripePaymentStatus: string
stripeId: string
stripePriceId: Array<string>
stripeSubscriptionStartDate: string
stripeSubscriptionEndDate: string
sessionPhoto: ISessionPhoto | null
madeFrom: EMadeFrom
createdAt: Date
updatedAt: Date
}
the route:
const CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL =
'customer.subscription.updated.trial'
try {
const payment = await modelModel.createPayment(
model.id,
stripeInvoice.total / 100,
stripeCustomer.invoice_settings.default_payment_method,
'card',
productIsBundle ? BUNDLE : SUBSCRIPTION,
subscriptionUpdated.customer,
subscriptionUpdated.status,
subscriptionUpdated.id,
subscriptionUpdated.plan.id,
stripeSubscriptionStartDate,
stripeSubscriptionEndDate,
sessionPhoto,
CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL,
)
and my createPayment function:
const createPayment = (
modelId: IPayments['modelID'],
amount: IPayments['amount'],
paymentMethod: IPayments['paymentMethod'],
paymentMethodType: IPayments['paymentMethodType'],
productType: IPayments['productType'],
stripeCustomerId: IPayments['stripeCustomerId'],
stripePaymentStatus: IPayments['stripePaymentStatus'],
stripeId: IPayments['stripeId'],
stripePriceId: IPayments['stripePriceId'],
stripeSubscriptionStartDate: IPayments['stripeSubscriptionStartDate'],
stripeSubscriptionEndDate: IPayments['stripeSubscriptionEndDate'],
sessionPhoto: null | IPayments['sessionPhoto'],
madeFrom: IPayments['madeFrom'],
) =>
new Promise((resolve, reject) => {
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经定义了
customer_subscription_updated_trial
作为:这是 String ,而不是您的
emade> emadefrom
enum的成员。您可以通过索引中将其转换为Emade
的类型:因此,您实际上会调用您的功能:实际上:
You've defined
CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL
as this:This is a string, not a member of your
EMadeFrom
enum. You can convert it to the type ofEMadeFrom
by indexing into it:So you would call your function like this, actually: