Skip to content

Use type instead of enum #284

@shwao

Description

@shwao

Some things that could be a TypeScript type are enum which is quite developer unfriendly. Code completion and many checks just don't work.

The Problem

For example if you have a Payment and want to check its status, the IDE (VS Code in my case) can't give you anything to autocomplete. The type check works though.

Here you should have the statuses as suggestions:
Screenshot 2022-07-21 at 14 48 58

This is the case for every enum including Locale, PaymentMethod etc.

While the missing suggestions are basically just an inconvenience (and missing out on fully utilizing TypeScript and the IDE) using the embed option of the mollieClient.payments.get method just does not work without using any because of the enum:

Screenshot 2022-07-21 at 14 51 51

In contrast to the include option which uses a type:

Screenshot 2022-07-21 at 14 55 10

Changing the PaymentEmbed from an enum to type fixes this:

Screenshot 2022-07-21 at 15 01 49
Screenshot 2022-07-21 at 15 03 16

Suggested Changes

Looking at the source code it seems like this change could be easy.

Lets use the PaymentStatus as an example.

The PaymentStatus enum from src/data/payments/data.ts:825 is used like an Object in src/data/payments/PaymentHelper.ts to check against the payment status.

I would suggest to change src/data/payments/data.ts from this:

export enum PaymentStatus {
  open = 'open',
  canceled = 'canceled',
  pending = 'pending',
  authorized = 'authorized',
  expired = 'expired',
  failed = 'failed',
  paid = 'paid',
}

To this:

export const PAYMENT_STATUS = {
  open: 'open',
  canceled: 'canceled',
  pending: 'pending',
  authorized: 'authorized',
  expired: 'expired',
  failed: 'failed',
  paid: 'paid',
};

export type PaymentStatus = keyof typeof PAYMENT_STATUS;

And then use it in src/data/payments/PaymentHelper.ts like this:

  public isOpen(this: PaymentData): boolean {
    return this.status === PAYMENT_STATUS.open;
  }

The PaymentStatus is now a type that can be used without problems.

Screenshot 2022-07-21 at 15 34 46

Conclusion

I don't know why you at Mollie use enums instead of types but looking at the code i don't see a good reason. Maybe i just didn't find it yet or maybe there is none. I just know that using the Mollie Node package would be better if they were types not enums.

If this change is welcome i would create a PR for that. But i would need some guidance regarding the API_KEY in the .env file (Just the regular API key or something special?) and regarding the amount of errors when running tsc --noEmit (Do i have to configure something special?). But I also do not insist on doing it myself. 😅

Metadata

Metadata

Assignees

Labels

need more infoThis needs to be discussed or investigated more.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions