Type System

Lets talk about they Type System that comes with Cratebox.

The type system is built for you to make sure everything is supposed to work as intended; for example, if you declare that some property of the model is of type string, and you try to set (accidentaly or not) a different type to it (with a small exception that we will explain later), let’s say, a number, it will complain that the received type is different from the expected.

Let’s get more into the Type System. We’re now going to explain the two different types we have in Cratebox.

Basic Types

The basic types are the core types of any (almost every) programming language. We use this types to build a solid structure for our data. Those types are the following:

String Type — this type can contain any value of type string and null.

this type can contain any value of type string and null. Number Type — this type can contain any value of type number and null.

this type can contain any value of type number and null. Boolean Type — this type can contain any value of type boolean.

this type can contain any value of type boolean. Null Type — this type can contain any value of type null.

this type can contain any value of type null. Undefined Type — this type can contain any value of type undefined.

this type can contain any value of type undefined. Date Type — this type can contain any value of type Date.

With this basic types you can compose a simple structure of data with type-safe checks. Let’s see how this could be used in a minimal example:

{

name: types.string,

lastName: types.string,

email: types.string,

age: types.number,

birthDate: types.date,

admin: types.boolean,

}

Let’s examinate the previous structure, the object that you’re seeing there it’s what we call a model object. A model object is a structure of properties and types that we will declare as our structure for a certain store.

We can see that it has several properties, in this case, six of them, and each one is typed the way we wanted to be.

The name, lastName and email properties must contain a value of type string or null. The age property must contain a value of type number or null. The birthDate property must contain a value of type Date (do not confuse with a formatted date-like string, that won’t work) or null.

Advanced Types

Array Type — this type can contain an array of a specific base type or advanced type (like frozen) and by using the last, we will have a complex type .

this type can contain an array of a specific base type or advanced type (like frozen) and by using the last, we will have a . Literal Type — this type can only contain one string literal and will check everything against this literal to make sure it’s the one specified.

this type can only contain one string literal and will check everything against this literal to make sure it’s the one specified. Enum Type — this type can contain an array of literal strings that will serve as an enumeration for your desired purpose (generally used for UI views routing, for example). The Enum Type is an example complex type of an Array Type of Literal Types . And it’s built in for you out-of-the-box.

this type can contain an array of literal strings that will serve as an enumeration for your desired purpose (generally used for UI views routing, for example). The is an example complex type of an of . And it’s built in for you out-of-the-box. Frozen Type — this type will contain a model object structure (like the one we showcased before) that will do all the type-checks for each of the properties you declare when creating this type.

// Declare a TODO type

const todo = types.frozen({

id: types.number,

title: types.string,

description: types.string,

done: types.boolean

}); // Describe the TODOS store

crate.describeStore({

identifier: "todoStore",

model: {

todos: types.array(todo)

}

});

As you can see in the example above we’ve declared a Todo Type by using the Frozen Type. After that, we describe a store (don’t worry if you can’t understand this, I promise we will look at it in a few moments and everything will be cristal clear) and the model contains a property that’s called todos and that property is a Complex Type (as we learned before) because it’s an Array Type of Frozen Type.