Use types as constants within switch()?

  Kiến thức lập trình

I have some types in a checkoutTypes.ts file like so:

export type CheckoutInvoiceAddressSection = "InvoiceAddress";
export type CheckoutDeliveryAddressSection = "DeliveryAddress";
export type CheckoutDeliveryAndReviewSection = "DeliveryAndReview";

export type CheckoutSection = CheckoutInvoiceAddressSection | CheckoutDeliveryAddressSection | CheckoutDeliveryAndReviewSection;

In a function in a different file I want to use these for case matching in a switch() statement:

import type { CheckoutInvoiceAddressSection, CheckoutDeliveryAddressSection, CheckoutDeliveryAndReviewSection, CheckoutSection } from "~/types/checkoutTypes";

interface Props {
  identifier: CheckoutSection,
}
const props = defineProps<Props>();

const isSectionCompleted = computed(() => {
  switch (props.identifier) {
    case CheckoutInvoiceAddressSection:
      return checkoutStore.getActiveSection !== CheckoutInvoiceAddressSection;
    case CheckoutDeliveryAddressSection:
      return checkoutStore.getActiveSection === CheckoutDeliveryAndReviewSection;
  }
});

However, I’m getting the following error: ReferenceError: CheckoutInvoiceAddressSection is not defined. on the case CheckoutInvoiceAddressSection line.
Are type definitions not suitable to be used for comparison with a variable, or am I missing something else?

1

LEAVE A COMMENT