Skip to main content

Function: defineModelMixin()

Call Signature​

function defineModelMixin<MP>(props): ModelMixin<ModelPropsToTransformedData<MP>, unknown, MP>;

Defines a model mixin from a ModelProps object.

ModelData / ModelCreationData on the composed class resolve to the exact prop types.

Type Parameters​

Type ParameterDescription
MP extends ModelPropsModel properties type (inferred from props).

Parameters​

ParameterTypeDescription
propsMPModel properties object.

Returns​

ModelMixin<ModelPropsToTransformedData<MP>, unknown, MP>

Example​

const countableMixin = defineModelMixin({ quantity: tProp(types.number, 0) })

Call Signature​

function defineModelMixin<Req, MP>(props, requirement): ModelMixin<ModelPropsToTransformedData<MP>, Req, MP>;

Defines a model mixin from a ModelProps object, with a requirement on the base model.

Pass req<Req>() as the second argument to declare that the base model must already contain the Req shape before the mixin is applied.

Type Parameters​

Type ParameterDescription
Req extends objectShape that must exist on the base model instance (specified via req<Req>()).
MP extends ModelPropsModel properties type (inferred from props).

Parameters​

ParameterTypeDescription
propsMPModel properties object.
requirementReqMarker<Req>Requirement marker created by req<Req>().

Returns​

ModelMixin<ModelPropsToTransformedData<MP>, Req, MP>

Example​

const producerMixin = defineModelMixin(
{ produced: tProp(types.number, 0) },
req<{ quantity: number }>()
)

Call Signature​

function defineModelMixin<MP, C>(props, build): ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, unknown, MP>;

Defines a model mixin from a ModelProps object and a builder that can add actions and other methods.

The props are applied via ExtendedModel first; the resulting pre-extended class is passed to the builder so you can simply extend Base without calling ExtendedModel yourself. ModelData / ModelCreationData resolve to the exact prop types from MP.

Type Parameters​

Type ParameterDescription
MP extends ModelPropsModel properties type (inferred from props).
C extends AbstractModelClass<AnyModel & ModelPropsToTransformedData<MP>>Built class type (inferred from build return type).

Parameters​

ParameterTypeDescription
propsMPModel properties object.
build(base) => CBuilder receiving the pre-extended base class.

Returns​

ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, unknown, MP>

Example​

const countableMixin = defineModelMixin(
{ quantity: tProp(types.number, 0) },
(Base) => class Countable extends Base {
increment() { this.quantity++ }
}
)

Call Signature​

function defineModelMixin<Req, MP, C>(
props,
requirement,
build
): ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, Req, MP>;

Defines a model mixin from a ModelProps object and a builder, with a requirement on the base model.

Pass req<Req>() as the second argument to declare that the base model must already contain the Req shape. The Base class received by the builder includes both the prop types and Req.

Type Parameters​

Type ParameterDescription
Req extends objectShape that must exist on the base model instance (specified via req<Req>()).
MP extends ModelPropsModel properties type (inferred from props).
C extends AbstractModelClass<AnyModel & ModelPropsToTransformedData<MP>>Built class type (inferred from build return type).

Parameters​

ParameterTypeDescription
propsMPModel properties object.
requirementReqMarker<Req>Requirement marker created by req<Req>().
build(base) => CBuilder receiving the pre-extended base class (typed with both props and Req).

Returns​

ModelMixin<Omit<InstanceType<C>, BaseModelKeys>, Req, MP>

Example​

const producerMixin = defineModelMixin(
{ produced: tProp(types.number, 0) },
req<{ quantity: number }>(),
(Base) => class Producer extends Base {
produceTotal() { return this.produced + this.quantity }
}
)