Types Overview

These are the types available in MST. All types can be found in the types namespace, e.g. types.string.

Complex Types

Type Description
types.model(properties, action) Defines a "class-like" type, with properties and actions to operate on the object.
types.array(type) Declares an array of the specified type.
types.map(type) Declares a map of the specified type.

Primitive Types

Type Description
types.string String
types.number Number
types.boolean Boolean
types.Date Date

Utility Types

Type Description
types.union(dispatcher?, types...) Creates a union of multiple types. If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function of the form (snapshot) => Type.
types.optional(type, defaultValue) Marks a value as being optional (e.g. in a model). If a value is not provided the defaultValue will be used instead. If defaultValue is a function, it will be evaluated. This can be used to generate, for example, IDs or timestamps upon creation.
types.literal(value) Creates a literal type, where the only possible value is specifically that value. This is very powerful in combination with unions (e.g. temperature: types.union(types.literal("hot"), types.literal("cold"))).
types.enumeration(name?, options: string[]) Creates an enumeration. This method is shorthand for a union of string literals.
types.refinement(name?, baseType, (snapshot) => boolean) Creates a type that is more specific than the base type (e.g. types.refinement(types.string, value => value.length > 5) to create a type of strings that can only be longer than 5).
types.maybe(type) Makes a type optional and nullable, shorthand for types.optional(types.union(type, types.literal(null), null).
types.null The type of null.
types.undefined The type of undefined.
types.late(() => type) Creates recursive or circular types, or types that are spread over files in such a way that circular dependencies between files would be an issue otherwise.
types.frozen Accepts any kind of serializable value (both primitive and complex), but assumes that the value itself is immutable and serializable.
types.compose(name?, type1...typeX) Creates a new model type by taking a bunch of existing types and combining them into a new one.

Property Types

Property types can only be used as a direct member of a types.model type, and not further composed (for now).

Type Description
types.identifier(subType?) Only one such member can exist in a types.model and should uniquely identify the object. subType should be either types.string or types.number, defaulting to the first if not specified.
types.reference(targetType) Creates a property that is a reference to another item of the given targetType somewhere in the same tree.

results matching ""

    No results matching ""