To install the module from npm:
npm install --save tiny-types
API documentation is available at jan-molak.github.io/tiny-types/.
TinyTypes are available as part of the Tidelift Subscription. The maintainers of TinyTypes and thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. If you want the flexibility of open source and the confidence of commercial-grade software, this is for you. Learn more.
Defining Tiny Types
An int on its own is just a scalar with no meaning. With an object, even a small one, you are giving both the compiler and the programmer additional information about what the value is and why it is being used.
To define a single-value
TinyType - extend from
Every tiny type defined this way has
a readonly property
value of type
T, which you can use to access the wrapped primitive value. For example:
;firstName.value === 'Jan';
Each tiny type object has an
equals method, which you can use to compare it by value:
name1 = new FirstName'Jan',name2 = new FirstName'Jan';name1.equalsname2 === true;
An additional feature of tiny types is a built-in
;name.toString === 'FirstName(value=Jan)';
Which you can override if you want to:
;timestampt.toString === 'Timestamp(value=2018-03-12T00:30:00.000Z))'
Multi-value and complex types
If the tiny type you want to model has more than one value,
or you want to perform additional operations in the constructor,
You can also mix and match both of the above definition styles:
Even such complex types still have both the
now = new Date2018, 2, 12, 0, 30,event1 = new AccountCreatednew UserName'jan-molak', new Timestampnow,event2 = new AccountCreatednew UserName'jan-molak', new Timestampnow;event1.equalsevent2 === true;event1.toString === 'AccountCreated(username=UserName(value=jan-molak), value=Timestamp(value=2018-03-12T00:30:00.000Z))'
Guaranteed runtime correctness
The best way to guarantee runtime correctness of your domain models is to ensure that no tiny type can ever hold invalid data at runtime. This way, when a function receives an instance of a tiny type, it does not need to perform any checks on it and can simply trust that its value is correct. OK, but how do you guarantee that?
Let me show you an example.
Imagine that upon registering a customer on your website you need to ask them their age. How would you model the concept of "age" in your system?
You might consider using a
number for this purpose:
However, this is far from ideal as "age" is not just any number: it can't be negative, it has to be an integer, and it's highly unlikely that your customers would ever be 253-1 years old.
All that means that there are certain rules that an object representing "age" needs to obey, certain constraints that its value has to meet in order to be considered valid.
You might have already guessed that my recommendation to you would be to define a tiny type representing
Age, but not just that.
You should also take it a step further and use the
ensure function together with other
predicates to describe the constraints the underlying value has to meet:
With a tiny type defined as per the above code sample you can eliminate entire classes of errors. You also have one place in your system where you define what "age" means.
Serialisation to JSON
Single-value TinyTypes are serialised to the value itself:
;;firstName.toJSON === 'Jan'
Complex TinyTypes are serialised recursively:
De-serialisation from JSON
Although you could define standalone de-serialisers, I like to define them as static factory methods on the TinyTypes themselves:
;FirstName.fromJSONfirstName.toJSON.equalsfirstName === true
When working with complex TinyTypes, you can use the (experimental)
to reduce the likelihood of your custom
fromJSON method being incompatible with
This way de-serialising a complex type becomes trivial:
;;allocation.equalsdeserialised === true
Serialised is by no means 100% foolproof as it's only limited to checking whether your input JSON has the same fields
as the object you're trying to de-serialise, it can at least help you to avoid errors caused by typos.
Your feedback matters!
Do you find TinyTypes useful? Give it a star! ★
Found a bug? Need a feature? Raise an issue or submit a pull request.
Have feedback? Let me know on twitter: @JanMolak
Before you go
TinyTypes library is licensed under the Apache-2.0 license.
- Copyright © 2018- Jan Molak