- Published on
Stronger JavaScript with Opaque Types
- Authors
- Name
- Lucas Floriani
- @lucasfloriani13
Stronger JavaScript with Opaque Types
Stronger JavaScript with Opaque Types
What is Opaque Types?
Is a way to enforce in the types that we are using a correct type that was declared for the context, even though the internal type we are passing can be equal (number for example).
Example of a code that doesnt ensure the correct usage of parameters because of the lack of Opaque Types:
This code is passing the amount first, even tought it expected the accountNumber
To ensure something like this in the Type level, we can use Opaque Types
How to declare it?
A generic to abstract the logic for us:
How it affects the code?
Check this example bellow:
Using the code above in an application: