如何使用枚举在打字稿中键入

发布于 2025-01-31 18:36:55 字数 2323 浏览 6 评论 0原文

我对如何与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-02-07 18:36:55

您已经定义了customer_subscription_updated_trial作为:

const CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL = 'customer.subscription.updated.trial';

这是 String ,而不是您的emade> emadefrom enum的成员。您可以通过索引中将其转换为Emade的类型:

EMadeFrom[CUSTOM_SUBSCRIPTION_UPDATED_TRIAL]; // gives correct value

因此,您实际上会调用您的功能:实际上:

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,
    EMadeFrom[CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL],
)

You've defined CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL as this:

const CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL = 'customer.subscription.updated.trial';

This is a string, not a member of your EMadeFrom enum. You can convert it to the type of EMadeFrom by indexing into it:

EMadeFrom[CUSTOM_SUBSCRIPTION_UPDATED_TRIAL]; // gives correct value

So you would call your function like this, actually:

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,
    EMadeFrom[CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL],
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文