Published on

Stronger JavaScript with Opaque Types

Authors

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:

Fallback text 1
Fallback text 2

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?

Fallback text 3

A generic to abstract the logic for us:

Fallback text 4

How it affects the code?

Check this example bellow:

Fallback text 5

Using the code above in an application:

Fallback text 6