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

How to use enums for typing in typescript

我对如何在 TS 中使用枚举有点困惑。您可以使用枚举作为值而不是类型吗? 我尝试了很多不同的方法,但我无法解决它。 这是我的代码,但它似乎没有类型检查:

这是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
}

路线:

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,
                  )

和我的 createPayment 函数:

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) => {
...

您已将 CUSTOMER_SUBSCRIPTION_UPDATED_TRIAL 定义为:

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

这是一个 字符串 ,不是您的 EMadeFrom 枚举的成员。您可以通过索引将其转换为 EMadeFrom 的类型:

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],
)