Const
A type that represents a data model data. The type referenced in the model decorator will be used for type checking.
Example:
const someDataModelDataType = types.dataModelData(SomeModel)
// or for recursive models
const someDataModelDataType = types.dataModelData<SomeModel>(() => SomeModel)
An enum type, based on a TypeScript alike enum object.
Syntactic sugar for types.or(...enum_values.map(types.literal))
Example:
enum Color {
Red = "red",
Green = "green"
}
const colorType = types.enum(Color)
A type that represents frozen data.
Example:
const frozenNumberType = types.frozen(types.number)
const frozenAnyType = types.frozen(types.unchecked<any>())
const frozenNumberArrayType = types.frozen(types.array(types.number))
const frozenUncheckedNumberArrayType = types.frozen(types.unchecked<number[]>())
Type of the frozen data.
A type that represents a certain value of a primitive (for example an exact number or string).
Example
const hiType = types.literal("hi") // the string with value "hi"
const number5Type = types.literal(5) // the number with value 5
Literal value.
A type that represents either a type or undefined.
Syntactic sugar for types.or(baseType, types.undefined)
Example:
const numberOrUndefinedType = types.maybe(types.number)
Type.
A type that represents either a type or null.
Syntactic sugar for types.or(baseType, types.null)
const numberOrNullType = types.maybeNull(types.number)
Type.
A type that represents a model. The type referenced in the model decorator will be used for type checking.
Example:
const someModelType = types.model(SomeModel)
// or for recursive models
const someModelType = types.model<SomeModel>(() => SomeModel)
A type that represents a plain object. Note that the parameter must be a function that returns an object. This is done so objects can support self / cross types.
Example:
// notice the ({ ... }), not just { ... }
const pointType = types.object(() => ({
x: types.number,
y: types.number
}))
Function that generates an object with types.
A type that represents the union of several other types (a | b | c | ...). Accepts a dispatcher that, given a snapshot, returns the type that snapshot is.
A type that represents the union of several other types (a | b | c | ...).
Example:
const booleanOrNumberType = types.or(types.boolean, types.number)
Rest
...orTypes: TPossible types.
A type that represents an object-like map, an object with string keys and values all of a same given type.
Example:
// { [k: string]: number }
const numberMapType = types.record(types.number)
Type of the values of the object-like map.
A refinement over a given type. This allows you to do extra checks over models, ensure numbers are integers, etc.
Example:
const integerType = types.refinement(types.number, (n) => {
return Number.isInteger(n)
}, "integer")
const sumModelType = types.refinement(types.model(Sum), (sum) => {
// imagine that for some reason sum includes a number 'a', a number 'b'
// and the result
const rightResult = sum.a + sum.b === sum.result
// simple mode that will just return that the whole model is incorrect
return rightResult
// this will return that the result field is wrong
return rightResult ? null : new TypeCheckError(["result"], "a+b", sum.result)
})
Base type.
Function that will receive the data (if it passes the base type check) and return null or false if there were no errors or either a TypeCheckError instance or true if there were.
Optional
typeName: stringWrap a given type with tag information. This allows you to associate metadata with the type of a prop that you can use at runtime.
Example:
const widthType = types.tag(types.number, { displayName: "Width in Inches", required: true }, "dimension")
const heightType = types.tag(types.number, { displayName: "Height in Inches", required: true }, "dimension")
These can then be accessed at runtime through inspection APIs, e.g.
@model('MyModel')
class MyModel extends Model({
width: tProp(widthType, 10),
height: tProp(heightType, 10)
}) {}
const m = new MyModel({})
const type = types.model<typeof Model>(m.constructor)
const modelTypeInfo = getTypeInfo(type) as ModelTypeInfo
const propTypeInfo = modelTypeInfo.props.width.typeInfo as TagTypeInfo
const displayName = propTypeInfo.displayName
A type that represents a given value that won't be type checked. This is basically a way to bail out of the runtime type checking system.
Example:
const uncheckedSomeModel = types.unchecked<SomeModel>()
const anyType = types.unchecked<any>()
const customUncheckedType = types.unchecked<(A & B) | C>()
A type that represents an array of values of a given type.
Example: