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. |