-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use type instead of enum #284
Comments
Oh damn. I just realized it... You are supped to use the Enum. I still think its cumbersome and my idea is better but at least i can use |
Thanks for the issue. You are ‒ as you found out on your own ‒ supposed to use the enum in your code when writing TypeScript: mollieClient.payments.create({ method: PaymentMethod.ideal, … }) This makes code completion work, and eliminates the need for However, I personally do not necessarily favour enums over union types of string constants (e.g. .create({ method: PaymentMethod.ideal, … is not better or worse than: .create({ method: 'ideal', … (For the sake of completeness and to anyone reading this issue: if you're writing JavaScript, you can use either; the former is required when writing TypeScript.) As I don't really have a personal preference, I decided against introducing new enums for new parts of the API. (You've stumbled upon this in the form of This is also the reason I haven't seriously considered de-enumming I'll bump heads with some people about this and see what they have to say. Regarding Regarding the "amount of errors" you get when executing the TypeScript compiler, I would have to see those errors. I don't have enough information to help you at this point. |
It just occurred to me while making coffee. (Pauses are great) I think i just haven't seen an example on https://docs.mollie.com/ that uses these enums and didn't think of it before.
Kind off. Union types work better and faster as you don't need to import anything for it. Just type a quote and all the possible values are displayed. Also hovering over a union type does show you the possible values while the enum just shows the name Union type: Screen.Recording.2022-07-21.at.16.46.22.movVersus enum (ignore that Finger-Muscle-Memory-Error): Screen.Recording.2022-07-21.at.16.48.03.movAlso i think in most IDE themes strings are better visible than variables which makes the union type better visible (at least for me). I also realized that changing Okay thanks for your answer, i I look forward to what your colleagues say. In the meantime ill just use those enums. |
Assuming this is resolved. Feel free to reopen, if not. |
@Pimm, did you reopen this because the My two cents on the latter are this: We COULD change the type definitions to be this: {
method: `${PaymentStatus}`
} This effectively turns the enum into a string literal. The one downside I can think of is that the existence of those enums is now less obvious to a consumer (especially someone new to the SDK), because they're not nudged towards them by their IDE. So this does come down to personal preference a bit, I guess. |
Some things that could be a TypeScript
type
areenum
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:
This is the case for every
enum
includingLocale
,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 themollieClient.payments.get
method just does not work without usingany
because of theenum
:In contrast to the
include
option which uses atype
:Changing the
PaymentEmbed
from anenum
totype
fixes this: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
fromsrc/data/payments/data.ts:825
is used like an Object insrc/data/payments/PaymentHelper.ts
to check against the payment status.I would suggest to change
src/data/payments/data.ts
from this:To this:
And then use it in
src/data/payments/PaymentHelper.ts
like this:The
PaymentStatus
is now a type that can be used without problems.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. 😅The text was updated successfully, but these errors were encountered: